home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / dist / cccp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-09  |  142.4 KB  |  5,638 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2. Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.                     Written by Paul Rubin, June 1986
  4.             Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 1, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!  */
  23.  
  24. typedef unsigned char U_CHAR;
  25.  
  26. #ifdef EMACS
  27. #define NO_SHORTNAMES
  28. #include "../src/config.h"
  29. #ifdef open
  30. #undef open
  31. #undef read
  32. #undef write
  33. #endif /* open */
  34. #endif /* EMACS */
  35.  
  36. #ifndef EMACS
  37. #include "config.h"
  38. #endif /* not EMACS */
  39.  
  40. /* In case config.h defines these.  */
  41. #undef bcopy
  42. #undef bzero
  43. #undef bcmp
  44.  
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <ctype.h>
  48. #include <stdio.h>
  49. #include <signal.h>
  50.  
  51. #ifndef VMS
  52. #include <sys/file.h>
  53. #ifndef USG
  54. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  55. #include <sys/resource.h>
  56. #else
  57. #define index strchr
  58. #define rindex strrchr
  59. #include <time.h>
  60. #include <fcntl.h>
  61. #endif /* USG */
  62. #endif /* not VMS */
  63.   
  64. /* VMS-specific definitions */
  65. #ifdef VMS
  66. #include <time.h>
  67. #include <errno.h>        /* This defines "errno" properly */
  68. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  69. #define O_RDONLY    0    /* Open arg for Read/Only  */
  70. #define O_WRONLY    1    /* Open arg for Write/Only */
  71. #define read(fd,buf,size)    VAX11_C_read(fd,buf,size)
  72. #define write(fd,buf,size)    VAX11_C_write(fd,buf,size)
  73. #ifdef __GNUC__
  74. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  75. #endif /* __GNUC__ */
  76. #endif /* VMS */
  77.  
  78. #define max(a,b) ((a) > (b) ? (a) : (b))
  79.  
  80. /* External declarations.  */
  81.  
  82. void bcopy (), bzero ();
  83. int bcmp ();
  84. extern char *getenv ();
  85. extern char *version_string;
  86.  
  87. /* Forward declarations.  */
  88.  
  89. struct directive;
  90. struct file_buf;
  91. struct arglist;
  92. struct argdata;
  93.  
  94. int do_define (), do_line (), do_include (), do_undef (), do_error (),
  95.   do_pragma (), do_if (), do_xifdef (), do_else (),
  96.   do_elif (), do_endif (), do_sccs (), do_once ();
  97.  
  98. struct hashnode *install ();
  99. struct hashnode *lookup ();
  100.  
  101. char *xmalloc (), *xrealloc (), *xcalloc (), *savestring ();
  102. void fatal (), fancy_abort (), pfatal_with_name (), perror_with_name ();
  103.  
  104. void macroexpand ();
  105. void dump_all_macros ();
  106. void conditional_skip ();
  107. void skip_if_group ();
  108. void output_line_command ();
  109. /* Last arg to output_line_command.  */
  110. enum file_change_code {same_file, enter_file, leave_file};
  111.  
  112. int grow_outbuf ();
  113. int handle_directive ();
  114. void memory_full ();
  115.  
  116. U_CHAR *macarg1 ();
  117. char *macarg ();
  118.  
  119. U_CHAR *skip_to_end_of_comment ();
  120. U_CHAR *skip_quoted_string ();
  121.  
  122. #ifndef FATAL_EXIT_CODE
  123. #define FATAL_EXIT_CODE 33    /* gnu cc command understands this */
  124. #endif
  125.  
  126. #ifndef SUCCESS_EXIT_CODE
  127. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  128. #endif
  129.  
  130. /* Name under which this program was invoked.  */
  131.  
  132. char *progname;
  133.  
  134. /* Nonzero means handle C++ comment syntax and use
  135.    extra default include directories for C++.  */
  136.  
  137. int cplusplus;
  138.  
  139. /* Current maximum length of directory names in the search path
  140.    for include files.  (Altered as we get more of them.)  */
  141.  
  142. int max_include_len;
  143.  
  144. /* Nonzero means copy comments into the output file.  */
  145.  
  146. int put_out_comments = 0;
  147.  
  148. /* Nonzero means don't process the ANSI trigraph sequences.  */
  149.  
  150. int no_trigraphs = 0;
  151.  
  152. /* Nonzero means print the names of included files rather than
  153.    the preprocessed output.  1 means just the #include "...",
  154.    2 means #include <...> as well.  */
  155.  
  156. int print_deps = 0;
  157.  
  158. /* Nonzero means don't output line number information.  */
  159.  
  160. int no_line_commands;
  161.  
  162. /* Nonzero means inhibit output of the preprocessed text
  163.    and instead output the definitions of all user-defined macros
  164.    in a form suitable for use as input to cccp.  */
  165.  
  166. int dump_macros;
  167.  
  168. /* Nonzero means give all the error messages the ANSI standard requires.  */
  169.  
  170. int pedantic;
  171.  
  172. /* Nonzero means warn if slash-star appears in a comment.  */
  173.  
  174. int warn_comments;
  175.  
  176. /* Nonzero means warn if there are any trigraphs.  */
  177.  
  178. int warn_trigraphs;
  179.  
  180. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  181.  
  182. int traditional;
  183.  
  184. /* Nonzero causes output not to be done,
  185.    but directives such as #define that have side effects
  186.    are still obeyed.  */
  187.  
  188. int no_output;
  189.  
  190. /* I/O buffer structure.
  191.    The `fname' field is nonzero for source files and #include files
  192.    and for the dummy text used for -D and -U.
  193.    It is zero for rescanning results of macro expansion
  194.    and for expanding macro arguments.  */
  195. #define INPUT_STACK_MAX 200
  196. struct file_buf {
  197.   char *fname;
  198.   int lineno;
  199.   int length;
  200.   U_CHAR *buf;
  201.   U_CHAR *bufp;
  202.   /* Macro that this level is the expansion of.
  203.      Included so that we can reenable the macro
  204.      at the end of this level.  */
  205.   struct hashnode *macro;
  206.   /* Value of if_stack at start of this file.
  207.      Used to prohibit unmatched #endif (etc) in an include file.  */
  208.   struct if_stack *if_stack;
  209.   /* Object to be freed at end of input at this level.  */
  210.   U_CHAR *free_ptr;
  211. } instack[INPUT_STACK_MAX];
  212.  
  213. /* Current nesting level of input sources.
  214.    `instack[indepth]' is the level currently being read.  */
  215. int indepth = -1;
  216. #define CHECK_DEPTH(code) \
  217.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  218.     {                                    \
  219.       error_with_line (line_for_error (instack[indepth].lineno),    \
  220.                "macro or #include recursion too deep");        \
  221.       code;                                \
  222.     }
  223.  
  224. /* Current depth in #include directives that use <...>.  */
  225. int system_include_depth = 0;
  226.  
  227. typedef struct file_buf FILE_BUF;
  228.  
  229. /* The output buffer.  Its LENGTH field is the amount of room allocated
  230.    for the buffer, not the number of chars actually present.  To get
  231.    that, subtract outbuf.buf from outbuf.bufp. */
  232.  
  233. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  234. FILE_BUF outbuf;
  235.  
  236. /* Grow output buffer OBUF points at
  237.    so it can hold at least NEEDED more chars.  */
  238.  
  239. #define check_expand(OBUF, NEEDED)  \
  240.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  241.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  242.  
  243. struct file_name_list
  244.   {
  245.     struct file_name_list *next;
  246.     char *fname;
  247.   };
  248.  
  249. /* #include "file" looks in source file dir, then stack. */
  250. /* #include <file> just looks in the stack. */
  251. /* -I directories are added to the end, then the defaults are added. */
  252. struct file_name_list include_defaults[] =
  253.   {
  254. #ifndef VMS
  255.     { &include_defaults[1], GCC_INCLUDE_DIR },
  256.     { &include_defaults[2], "/usr/include" },
  257.     { 0, "/usr/local/include" }
  258. #else
  259.     { &include_defaults[1], "GNU_CC_INCLUDE:" },       /* GNU includes */
  260.     { &include_defaults[2], "SYS$SYSROOT:[SYSLIB.]" }, /* VAX-11 "C" includes */
  261.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  262. #endif /* VMS */
  263.   };
  264.  
  265. /* These are used instead of the above, for C++.  */
  266. struct file_name_list cplusplus_include_defaults[] =
  267.   {
  268. #ifndef VMS
  269.     /* Pick up GNU C++ specific include files.  */
  270.     { &cplusplus_include_defaults[1], GPLUSPLUS_INCLUDE_DIR },
  271.     /* Use GNU CC specific header files.  */
  272.     { &cplusplus_include_defaults[2], GCC_INCLUDE_DIR },
  273.     { 0, "/usr/include" }
  274. #else
  275.     { &cplusplus_include_defaults[1], "GNU_GXX_INCLUDE:" },
  276.     { &cplusplus_include_defaults[2], "GNU_CC_INCLUDE:" },
  277.     /* VAX-11 C includes */
  278.     { &cplusplus_include_defaults[3], "SYS$SYSROOT:[SYSLIB.]" },
  279.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  280. #endif /* VMS */
  281.   };
  282.  
  283. struct file_name_list *include = 0;    /* First dir to search */
  284.     /* First dir to search for <file> */
  285. struct file_name_list *first_bracket_include = 0;
  286. struct file_name_list *last_include = 0;    /* Last in chain */
  287.  
  288. /* List of included files that contained #once.  */
  289. struct file_name_list *dont_repeat_files = 0;
  290.  
  291. /* List of other included files.  */
  292. struct file_name_list *all_include_files = 0;
  293.  
  294. /* Structure allocated for every #define.  For a simple replacement
  295.    such as
  296.        #define foo bar ,
  297.    nargs = -1, the `pattern' list is null, and the expansion is just
  298.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  299.    e.g.,
  300.        #define getchar() getc (stdin) .
  301.    When there are args, the expansion is the replacement text with the
  302.    args squashed out, and the reflist is a list describing how to
  303.    build the output from the input: e.g., "3 chars, then the 1st arg,
  304.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  305.    The chars here come from the expansion.  Whatever is left of the
  306.    expansion after the last arg-occurrence is copied after that arg.
  307.    Note that the reflist can be arbitrarily long---
  308.    its length depends on the number of times the arguments appear in
  309.    the replacement text, not how many args there are.  Example:
  310.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  311.    pattern list
  312.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  313.    where (x, y) means (nchars, argno). */
  314.  
  315. typedef struct definition DEFINITION;
  316. struct definition {
  317.   int nargs;
  318.   int length;            /* length of expansion string */
  319.   U_CHAR *expansion;
  320.   struct reflist {
  321.     struct reflist *next;
  322.     char stringify;        /* nonzero if this arg was preceded by a
  323.                    # operator. */
  324.     char raw_before;        /* Nonzero if a ## operator before arg. */
  325.     char raw_after;        /* Nonzero if a ## operator after arg. */
  326.     int nchars;            /* Number of literal chars to copy before
  327.                    this arg occurrence.  */
  328.     int argno;            /* Number of arg to substitute (origin-0) */
  329.   } *pattern;
  330.   /* Names of macro args, concatenated in reverse order
  331.      with comma-space between them.
  332.      The only use of this is that we warn on redefinition
  333.      if this differs between the old and new definitions.  */
  334.   U_CHAR *argnames;
  335. };
  336.  
  337. /* different kinds of things that can appear in the value field
  338.    of a hash node.  Actually, this may be useless now. */
  339. union hashval {
  340.   int ival;
  341.   char *cpval;
  342.   DEFINITION *defn;
  343. };
  344.  
  345.  
  346. /* The structure of a node in the hash table.  The hash table
  347.    has entries for all tokens defined by #define commands (type T_MACRO),
  348.    plus some special tokens like __LINE__ (these each have their own
  349.    type, and the appropriate code is run when that type of node is seen.
  350.    It does not contain control words like "#define", which are recognized
  351.    by a separate piece of code. */
  352.  
  353. /* different flavors of hash nodes --- also used in keyword table */
  354. enum node_type {
  355.  T_DEFINE = 1,    /* the `#define' keyword */
  356.  T_INCLUDE,    /* the `#include' keyword */
  357.  T_IFDEF,    /* the `#ifdef' keyword */
  358.  T_IFNDEF,    /* the `#ifndef' keyword */
  359.  T_IF,        /* the `#if' keyword */
  360.  T_ELSE,    /* `#else' */
  361.  T_PRAGMA,    /* `#pragma' */
  362.  T_ELIF,    /* `#else' */
  363.  T_UNDEF,    /* `#undef' */
  364.  T_LINE,    /* `#line' */
  365.  T_ERROR,    /* `#error' */
  366.  T_ENDIF,    /* `#endif' */
  367.  T_SCCS,    /* `#sccs', used on system V.  */
  368.  T_IDENT,    /* `#ident', used on system V.  */
  369.  T_SPECLINE,    /* special symbol `__LINE__' */
  370.  T_DATE,    /* `__DATE__' */
  371.  T_FILE,    /* `__FILE__' */
  372.  T_BASE_FILE,    /* `__BASE_FILE__' */
  373.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  374.  T_VERSION,    /* `__VERSION__' */
  375.  T_TIME,    /* `__TIME__' */
  376.  T_CONST,    /* Constant value, used by `__STDC__' */
  377.  T_MACRO,    /* macro defined by `#define' */
  378.  T_DISABLED,    /* macro temporarily turned off for rescan */
  379.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  380.  T_UNUSED    /* Used for something not defined.  */
  381.  };
  382.  
  383. struct hashnode {
  384.   struct hashnode *next;    /* double links for easy deletion */
  385.   struct hashnode *prev;
  386.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  387.                    chain is kept, in case the node is the head
  388.                    of the chain and gets deleted. */
  389.   enum node_type type;        /* type of special token */
  390.   int length;            /* length of token, for quick comparison */
  391.   U_CHAR *name;            /* the actual name */
  392.   union hashval value;        /* pointer to expansion, or whatever */
  393. };
  394.  
  395. typedef struct hashnode HASHNODE;
  396.  
  397. /* Some definitions for the hash table.  The hash function MUST be
  398.    computed as shown in hashf () below.  That is because the rescan
  399.    loop computes the hash value `on the fly' for most tokens,
  400.    in order to avoid the overhead of a lot of procedure calls to
  401.    the hashf () function.  Hashf () only exists for the sake of
  402.    politeness, for use when speed isn't so important. */
  403.  
  404. #define HASHSIZE 1403
  405. HASHNODE *hashtab[HASHSIZE];
  406. #define HASHSTEP(old, c) ((old << 2) + c)
  407. #define MAKE_POS(v) (v & ~0x80000000) /* make number positive */
  408.  
  409. /* Symbols to predefine.  */
  410.  
  411. #ifdef CPP_PREDEFINES
  412. char *predefs = CPP_PREDEFINES;
  413. #else
  414. char *predefs = "";
  415. #endif
  416.  
  417. /* `struct directive' defines one #-directive, including how to handle it.  */
  418.  
  419. struct directive {
  420.   int length;            /* Length of name */
  421.   int (*func)();        /* Function to handle directive */
  422.   char *name;            /* Name of directive */
  423.   enum node_type type;        /* Code which describes which directive. */
  424.   char angle_brackets;        /* Nonzero => <...> is special.  */
  425.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  426.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  427. };
  428.  
  429. /* Here is the actual list of #-directives, most-often-used first.  */
  430.  
  431. struct directive directive_table[] = {
  432.   {  6, do_define, "define", T_DEFINE, 0, 1},
  433.   {  2, do_if, "if", T_IF},
  434.   {  5, do_xifdef, "ifdef", T_IFDEF},
  435.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  436.   {  5, do_endif, "endif", T_ENDIF},
  437.   {  4, do_else, "else", T_ELSE},
  438.   {  4, do_elif, "elif", T_ELIF},
  439.   {  4, do_line, "line", T_LINE},
  440.   {  7, do_include, "include", T_INCLUDE, 1},
  441.   {  5, do_undef, "undef", T_UNDEF},
  442.   {  5, do_error, "error", T_ERROR},
  443. #ifdef SCCS_DIRECTIVE
  444.   {  4, do_sccs, "sccs", T_SCCS},
  445. #endif
  446.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  447.   {  -1, 0, "", T_UNUSED},
  448. };
  449.  
  450. /* table to tell if char can be part of a C identifier. */
  451. U_CHAR is_idchar[256];
  452. /* table to tell if char can be first char of a c identifier. */
  453. U_CHAR is_idstart[256];
  454. /* table to tell if c is horizontal space.  */
  455. U_CHAR is_hor_space[256];
  456. /* table to tell if c is horizontal or vertical space.  */
  457. U_CHAR is_space[256];
  458.  
  459. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  460. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  461.   
  462. int errors = 0;            /* Error counter for exit code */
  463.  
  464. /* Zero means dollar signs are punctuation.
  465.    -$ stores 0; -traditional, stores 1.  Default is 1 for VMS, 0 otherwise.
  466.    This must be 0 for correct processing of this ANSI C program:
  467.     #define foo(a) #a
  468.     #define lose(b) foo(b)
  469.     #define test$
  470.     lose(test)    */
  471. #ifndef DOLLARS_IN_IDENTIFIERS
  472. #define DOLLARS_IN_IDENTIFIERS 0
  473. #endif
  474. int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  475.  
  476. FILE_BUF expand_to_temp_buffer ();
  477.  
  478. DEFINITION *collect_expansion ();
  479.  
  480. /* Stack of conditionals currently in progress
  481.    (including both successful and failing conditionals).  */
  482.  
  483. struct if_stack {
  484.   struct if_stack *next;    /* for chaining to the next stack frame */
  485.   char *fname;        /* copied from input when frame is made */
  486.   int lineno;            /* similarly */
  487.   int if_succeeded;        /* true if a leg of this if-group
  488.                     has been passed through rescan */
  489.   enum node_type type;        /* type of last directive seen in this group */
  490. };
  491. typedef struct if_stack IF_STACK_FRAME;
  492. IF_STACK_FRAME *if_stack = NULL;
  493.  
  494. /* Buffer of -M output.  */
  495.  
  496. char *deps_buffer;
  497.  
  498. /* Number of bytes allocated in above.  */
  499. int deps_allocated_size;
  500.  
  501. /* Number of bytes used.  */
  502. int deps_size;
  503.  
  504. /* Number of bytes since the last newline.  */
  505. int deps_column;
  506.  
  507. /* Nonzero means -I- has been seen,
  508.    so don't look for #include "foo" the source-file directory.  */
  509. int ignore_srcdir;
  510.  
  511. /* Handler for SIGPIPE.  */
  512.  
  513. static void
  514. pipe_closed ()
  515. {
  516.   fatal ("output pipe has been closed");
  517. }
  518.  
  519. int
  520. main (argc, argv)
  521.      int argc;
  522.      char **argv;
  523. {
  524.   int st_mode;
  525.   long st_size;
  526.   char *in_fname, *out_fname;
  527.   int f, i;
  528.   FILE_BUF *fp;
  529.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  530.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  531.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  532.   int inhibit_predefs = 0;
  533.   int no_standard_includes = 0;
  534.  
  535.   /* Non-0 means don't output the preprocessed program.  */
  536.   int inhibit_output = 0;
  537.  
  538.   /* Stream on which to print the dependency information.  */
  539.   FILE *deps_stream = 0;
  540.   /* Target-name to write with the dependency information.  */
  541.   char *deps_target = 0;
  542.  
  543. #ifdef RLIMIT_STACK
  544.   /* Get rid of any avoidable limit on stack size.  */
  545.   {
  546.     struct rlimit rlim;
  547.  
  548.     /* Set the stack limit huge so that alloca (particularly stringtab
  549.      * in dbxread.c) does not fail. */
  550.     getrlimit (RLIMIT_STACK, &rlim);
  551.     rlim.rlim_cur = rlim.rlim_max;
  552.     setrlimit (RLIMIT_STACK, &rlim);
  553.   }
  554. #endif /* RLIMIT_STACK defined */
  555.  
  556.   progname = argv[0];
  557. #ifdef VMS
  558.   {
  559.     /* Remove directories from PROGNAME.  */
  560.     char *s;
  561.     extern char *rindex ();
  562.  
  563.     progname = savestring (argv[0]);
  564.  
  565.     if (!(s = rindex (progname, ']')))
  566.       s = rindex (progname, ':');
  567.     if (s)
  568.       strcpy (progname, s+1);
  569.     if (s = rindex (progname, '.'))
  570.       *s = '\0';
  571.   }
  572. #endif
  573.  
  574.   in_fname = NULL;
  575.   out_fname = NULL;
  576.  
  577.   /* Initialize is_idchar to allow $.  */
  578.   dollars_in_ident = 1;
  579.   initialize_char_syntax ();
  580.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  581.  
  582.   no_line_commands = 0;
  583.   no_trigraphs = 1;
  584.   dump_macros = 0;
  585.   no_output = 0;
  586.   cplusplus = 0;
  587. #ifdef CPLUSPLUS
  588.   cplusplus = 1;
  589. #endif
  590.  
  591.   signal (SIGPIPE, pipe_closed);
  592.  
  593. #ifndef VMS
  594.   max_include_len
  595.     = max (max (sizeof (GCC_INCLUDE_DIR),
  596.         sizeof (GPLUSPLUS_INCLUDE_DIR)),
  597.        sizeof ("/usr/include/CC"));
  598. #else /* VMS */
  599.   max_include_len
  600.     = sizeof("SYS$SYSROOT:[SYSLIB.]");
  601. #endif /* VMS */
  602.  
  603.   bzero (pend_files, argc * sizeof (char *));
  604.   bzero (pend_defs, argc * sizeof (char *));
  605.   bzero (pend_undefs, argc * sizeof (char *));
  606.  
  607.   /* Process switches and find input file name.  */
  608.  
  609.   for (i = 1; i < argc; i++) {
  610.     if (argv[i][0] != '-') {
  611.       if (out_fname != NULL)
  612.     fatal ("Usage: %s [switches] input output", argv[0]);
  613.       else if (in_fname != NULL)
  614.     out_fname = argv[i];
  615.       else
  616.     in_fname = argv[i];
  617.     } else {
  618.       switch (argv[i][1]) {
  619.  
  620.       case 'i':
  621.     if (argv[i][2] != 0)
  622.       pend_files[i] = argv[i] + 2;
  623.     else if (i + 1 == argc)
  624.       fatal ("Filename missing after -i option");
  625.     else
  626.       pend_files[i] = argv[i+1], i++;
  627.     break;
  628.  
  629.       case 'o':
  630.     if (out_fname != NULL)
  631.       fatal ("Output filename specified twice");
  632.     if (i + 1 == argc)
  633.       fatal ("Filename missing after -o option");
  634.     out_fname = argv[++i];
  635.     if (!strcmp (out_fname, "-"))
  636.       out_fname = "";
  637.     break;
  638.  
  639.       case 'p':
  640.     pedantic = 1;
  641.     break;
  642.  
  643.       case 't':
  644.     if (!strcmp (argv[i], "-traditional")) {
  645.       traditional = 1;
  646.       dollars_in_ident = 1;
  647.     } else if (!strcmp (argv[i], "-trigraphs")) {
  648.       no_trigraphs = 0;
  649.     }
  650.     break;
  651.  
  652.       case '+':
  653.     cplusplus = 1;
  654.     break;
  655.  
  656.       case 'W':
  657.     if (!strcmp (argv[i], "-Wtrigraphs")) {
  658.       warn_trigraphs = 1;
  659.     }
  660.     if (!strcmp (argv[i], "-Wcomments"))
  661.       warn_comments = 1;
  662.     if (!strcmp (argv[i], "-Wcomment"))
  663.       warn_comments = 1;
  664.     if (!strcmp (argv[i], "-Wall")) {
  665.       warn_trigraphs = 1;
  666.       warn_comments = 1;
  667.     }
  668.     break;
  669.  
  670.       case 'M':
  671.     if (!strcmp (argv[i], "-M"))
  672.       print_deps = 2;
  673.     else if (!strcmp (argv[i], "-MM"))
  674.       print_deps = 1;
  675.     inhibit_output = 1;
  676.     break;
  677.  
  678.       case 'd':
  679.     dump_macros = 1;
  680.     no_output = 1;
  681.     break;
  682.  
  683.       case 'v':
  684.     fprintf (stderr, "GNU CPP version %s\n", version_string);
  685.     break;
  686.  
  687.       case 'D':
  688.     {
  689.       char *p, *p1;
  690.  
  691.       if (argv[i][2] != 0)
  692.         p = argv[i] + 2;
  693.       else if (i + 1 == argc)
  694.         fatal ("Macro name missing after -D option");
  695.       else
  696.         p = argv[++i];
  697.  
  698.       if ((p1 = (char *) index (p, '=')) != NULL)
  699.         *p1 = ' ';
  700.       pend_defs[i] = p;
  701.     }
  702.     break;
  703.  
  704.       case 'U':        /* JF #undef something */
  705.     if (argv[i][2] != 0)
  706.       pend_undefs[i] = argv[i] + 2;
  707.     else if (i + 1 == argc)
  708.       fatal ("Macro name missing after -U option");
  709.     else
  710.       pend_undefs[i] = argv[i+1], i++;
  711.     break;
  712.  
  713.       case 'C':
  714.     put_out_comments = 1;
  715.     break;
  716.  
  717.       case 'E':            /* -E comes from cc -E; ignore it.  */
  718.     break;
  719.  
  720.       case 'P':
  721.     no_line_commands = 1;
  722.     break;
  723.  
  724.       case '$':            /* Don't include $ in identifiers.  */
  725.     dollars_in_ident = 0;
  726.     break;
  727.  
  728.       case 'I':            /* Add directory to path for includes.  */
  729.     {
  730.       struct file_name_list *dirtmp;
  731.  
  732.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
  733.         ignore_srcdir = 1;
  734.       else {
  735.         dirtmp = (struct file_name_list *)
  736.           xmalloc (sizeof (struct file_name_list));
  737.         dirtmp->next = 0;        /* New one goes on the end */
  738.         if (include == 0)
  739.           include = dirtmp;
  740.         else
  741.           last_include->next = dirtmp;
  742.         last_include = dirtmp;    /* Tail follows the last one */
  743.         if (argv[i][2] != 0)
  744.           dirtmp->fname = argv[i] + 2;
  745.         else if (i + 1 == argc)
  746.           fatal ("Directory name missing after -I option");
  747.         else
  748.           dirtmp->fname = argv[++i];
  749.         if (strlen (dirtmp->fname) > max_include_len)
  750.           max_include_len = strlen (dirtmp->fname);
  751.         if (ignore_srcdir && first_bracket_include == 0)
  752.           first_bracket_include = dirtmp;
  753.         }
  754.     }
  755.     break;
  756.  
  757.       case 'n':
  758.     /* -nostdinc causes no default include directories.
  759.        You must specify all include-file directories with -I.  */
  760.     no_standard_includes = 1;
  761.     break;
  762.  
  763.       case 'u':
  764.     /* Sun compiler passes undocumented switch "-undef".
  765.        Let's assume it means to inhibit the predefined symbols.  */
  766.     inhibit_predefs = 1;
  767.     break;
  768.  
  769.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  770.     if (in_fname == NULL) {
  771.       in_fname = "";
  772.       break;
  773.     } else if (out_fname == NULL) {
  774.       out_fname = "";
  775.       break;
  776.     }    /* else fall through into error */
  777.  
  778.       default:
  779.     fatal ("Invalid option `%s'", argv[i]);
  780.       }
  781.     }
  782.   }
  783.  
  784.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  785.   initialize_char_syntax ();
  786.  
  787.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  788.      and option processing.  */
  789.   initialize_builtins ();
  790.  
  791.   /* Do standard #defines that identify processor type.  */
  792.  
  793.   if (!inhibit_predefs) {
  794.     char *p = (char *) alloca (strlen (predefs) + 1);
  795.     strcpy (p, predefs);
  796.     while (*p) {
  797.       char *q;
  798.       if (p[0] != '-' || p[1] != 'D')
  799.     abort ();
  800.       q = &p[2];
  801.       while (*p && *p != ' ') p++;
  802.       if (*p != 0)
  803.     *p++= 0;
  804.       make_definition (q);
  805.     }
  806.   }
  807.  
  808.   /* Do defines specified with -D.  */
  809.   for (i = 1; i < argc; i++)
  810.     if (pend_defs[i])
  811.       make_definition (pend_defs[i]);
  812.  
  813.   /* Do undefines specified with -U.  */
  814.   for (i = 1; i < argc; i++)
  815.     if (pend_undefs[i])
  816.       make_undef (pend_undefs[i]);
  817.  
  818.   /* Unless -fnostdinc,
  819.      tack on the standard include file dirs to the specified list */
  820.   if (!no_standard_includes) {
  821.     if (include == 0)
  822.       include = (cplusplus ? cplusplus_include_defaults : include_defaults);
  823.     else
  824.       last_include->next
  825.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  826.     /* Make sure the list for #include <...> also has the standard dirs.  */
  827.     if (ignore_srcdir && first_bracket_include == 0)
  828.       first_bracket_include
  829.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  830.   }
  831.  
  832.   /* Initialize output buffer */
  833.  
  834.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  835.   outbuf.bufp = outbuf.buf;
  836.   outbuf.length = OUTBUF_SIZE;
  837.  
  838.   /* Scan the -i files before the main input.
  839.      Much like #including them, but with no_output set
  840.      so that only their macro definitions matter.  */
  841.  
  842.   no_output++;
  843.   for (i = 1; i < argc; i++)
  844.     if (pend_files[i]) {
  845.       int fd = open (pend_files[i], O_RDONLY, 0666);
  846.       if (fd < 0) {
  847.     perror_with_name (pend_files[i]);
  848.     return FATAL_EXIT_CODE;
  849.       }
  850.       finclude (fd, pend_files[i], &outbuf);
  851.     }
  852.   no_output--;
  853.  
  854.   /* Create an input stack level for the main input file
  855.      and copy the entire contents of the file into it.  */
  856.  
  857.   fp = &instack[++indepth];
  858.  
  859.   /* JF check for stdin */
  860.   if (in_fname == NULL || *in_fname == 0) {
  861.     in_fname = "";
  862.     f = 0;
  863.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  864.     goto perror;
  865.  
  866.   /* Either of two environment variables can specify output of deps.
  867.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  868.      where OUTPUT_FILE is the file to write deps info to
  869.      and DEPS_TARGET is the target to mention in the deps.  */
  870.  
  871.   if (print_deps == 0
  872.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  873.       || getenv ("DEPENDENCIES_OUTPUT") != 0))
  874.     {
  875.       char *spec = getenv ("DEPENDENCIES_OUTPUT");
  876.       char *s;
  877.       char *output_file;
  878.  
  879.       if (spec == 0)
  880.     {
  881.       spec = getenv ("SUNPRO_DEPENDENCIES");
  882.       print_deps = 2;
  883.     }
  884.       else
  885.     print_deps = 1;
  886.  
  887.       s = spec;
  888.       /* Find the space before the DEPS_TARGET, if there is one.  */
  889.       /* Don't use `index'; that causes trouble on USG.  */
  890.       while (*s != 0 && *s != ' ') s++;
  891.       if (*s != 0)
  892.     {
  893.       deps_target = s + 1;
  894.       output_file = (char *) xmalloc (s - spec + 1);
  895.       bcopy (spec, output_file, s - spec);
  896.       output_file[s - spec] = 0;
  897.     }
  898.       else
  899.     {
  900.       deps_target = 0;
  901.       output_file = spec;
  902.     }
  903.       
  904.       deps_stream = fopen (output_file, "a");
  905.       if (deps_stream == 0)
  906.     pfatal_with_name (output_file);
  907.     }
  908.   /* If the -M option was used, output the deps to standard output.  */
  909.   else if (print_deps)
  910.     deps_stream = stdout;
  911.  
  912.   /* For -M, print the expected object file name
  913.      as the target of this Make-rule.  */
  914.   if (print_deps) {
  915.     deps_allocated_size = 200;
  916.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  917.     deps_buffer[0] = 0;
  918.     deps_size = 0;
  919.     deps_column = 0;
  920.  
  921.     if (deps_target) {
  922.       deps_output (deps_target, 0);
  923.       deps_output (":", 0);
  924.     } else if (*in_fname == 0)
  925.       deps_output ("-: ", 0);
  926.     else {
  927.       int len;
  928.       char *p = in_fname;
  929.       char *p1 = p;
  930.       /* Discard all directory prefixes from P.  */
  931.       while (*p1) {
  932.     if (*p1 == '/')
  933.       p = p1 + 1;
  934.     p1++;
  935.       }
  936.       /* Output P, but remove known suffixes.  */
  937.       len = strlen (p);
  938.       if (p[len - 2] == '.' && (p[len - 1] == 'c' || p[len - 1] == 'C'))
  939.     deps_output (p, len - 2);
  940.       else if (p[len - 3] == '.'
  941.            && p[len - 2] == 'c'
  942.            && p[len - 1] == 'c')
  943.     deps_output (p, len - 3);
  944.       else
  945.     deps_output (p, 0);
  946.       /* Supply our own suffix.  */
  947.       deps_output (".o : ", 0);
  948.       deps_output (in_fname, 0);
  949.       deps_output (" ", 0);
  950.     }
  951.   }
  952.  
  953.   file_size_and_mode (f, &st_mode, &st_size);
  954.   fp->fname = in_fname;
  955.   fp->lineno = 1;
  956.   /* JF all this is mine about reading pipes and ttys */
  957.   if ((st_mode & S_IFMT) != S_IFREG) {
  958.     /* Read input from a file that is not a normal disk file.
  959.        We cannot preallocate a buffer with the correct size,
  960.        so we must read in the file a piece at the time and make it bigger.  */
  961.     int size;
  962.     int bsize;
  963.     int cnt;
  964.     U_CHAR *bufp;
  965.  
  966.     bsize = 2000;
  967.     size = 0;
  968.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  969.     bufp = fp->buf;
  970.     for (;;) {
  971.       cnt = read (f, bufp, bsize - size);
  972.       if (cnt < 0) goto perror;    /* error! */
  973.       if (cnt == 0) break;    /* End of file */
  974.       size += cnt;
  975.       bufp += cnt;
  976.       if (bsize == size) {    /* Buffer is full! */
  977.         bsize *= 2;
  978.         fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  979.     bufp = fp->buf + size;    /* May have moved */
  980.       }
  981.     }
  982.     fp->length = size;
  983.   } else {
  984.     /* Read a file whose size we can determine in advance.
  985.        For the sake of VMS, st_size is just an upper bound.  */
  986.     long i;
  987.     fp->length = 0;
  988.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  989.  
  990.     while (st_size > 0) {
  991.       i = read (f, fp->buf + fp->length, st_size);
  992.       if (i <= 0) {
  993.         if (i == 0) break;
  994.     goto perror;
  995.       }
  996.       fp->length += i;
  997.       st_size -= i;
  998.     }
  999.   }
  1000.   fp->bufp = fp->buf;
  1001.   fp->if_stack = if_stack;
  1002.   
  1003.   /* Unless inhibited, convert trigraphs in the input.  */
  1004.  
  1005.   if (!no_trigraphs)
  1006.     trigraph_pcp (fp);
  1007.  
  1008.   /* Make sure data ends with a newline.  And put a null after it.  */
  1009.  
  1010.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  1011.     fp->buf[fp->length++] = '\n';
  1012.   fp->buf[fp->length] = '\0';
  1013.  
  1014.   /* Now that we know the input file is valid, open the output.  */
  1015.  
  1016.   if (!out_fname || !strcmp (out_fname, ""))
  1017.     out_fname = "stdout";
  1018.   else if (! freopen (out_fname, "w", stdout))
  1019.     pfatal_with_name (out_fname);
  1020.  
  1021.   output_line_command (fp, &outbuf, 0, same_file);
  1022.  
  1023.   /* Scan the input, processing macros and directives.  */
  1024.  
  1025.   rescan (&outbuf, 0);
  1026.  
  1027.   /* Now we have processed the entire input
  1028.      Write whichever kind of output has been requested.  */
  1029.  
  1030.  
  1031.   if (dump_macros)
  1032.     dump_all_macros ();
  1033.   else if (! inhibit_output && deps_stream != stdout) {
  1034.     if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
  1035.       fatal ("I/O error on output");
  1036.   }
  1037.  
  1038.   if (print_deps) {
  1039.     fputs (deps_buffer, deps_stream);
  1040.     putc ('\n', deps_stream);
  1041.     if (deps_stream != stdout) {
  1042.       fclose (deps_stream);
  1043.       if (ferror (deps_stream))
  1044.     fatal ("I/O error on output");
  1045.     }
  1046.   }
  1047.  
  1048.   if (ferror (stdout))
  1049.     fatal ("I/O error on output");
  1050.  
  1051.   if (errors)
  1052.     exit (FATAL_EXIT_CODE);
  1053.   exit (SUCCESS_EXIT_CODE);
  1054.  
  1055.  perror:
  1056.   pfatal_with_name (in_fname);
  1057. }
  1058.  
  1059. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  1060.    before main CCCP processing.  Name `pcp' is also in honor of the
  1061.    drugs the trigraph designers must have been on.
  1062.  
  1063.    Using an extra pass through the buffer takes a little extra time,
  1064.    but is infinitely less hairy than trying to handle ??/" inside
  1065.    strings, etc. everywhere, and also makes sure that trigraphs are
  1066.    only translated in the top level of processing. */
  1067.  
  1068. trigraph_pcp (buf)
  1069.      FILE_BUF *buf;
  1070. {
  1071.   register U_CHAR c, *fptr, *bptr, *sptr;
  1072.   int len;
  1073.  
  1074.   fptr = bptr = sptr = buf->buf;
  1075.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  1076.     if (*++sptr != '?')
  1077.       continue;
  1078.     switch (*++sptr) {
  1079.       case '=':
  1080.       c = '#';
  1081.       break;
  1082.     case '(':
  1083.       c = '[';
  1084.       break;
  1085.     case '/':
  1086.       c = '\\';
  1087.       break;
  1088.     case ')':
  1089.       c = ']';
  1090.       break;
  1091.     case '\'':
  1092.       c = '^';
  1093.       break;
  1094.     case '<':
  1095.       c = '{';
  1096.       break;
  1097.     case '!':
  1098.       c = '|';
  1099.       break;
  1100.     case '>':
  1101.       c = '}';
  1102.       break;
  1103.     case '-':
  1104.       c  = '~';
  1105.       break;
  1106.     case '?':
  1107.       sptr--;
  1108.       continue;
  1109.     default:
  1110.       continue;
  1111.     }
  1112.     len = sptr - fptr - 2;
  1113.     if (bptr != fptr && len > 0)
  1114.       bcopy (fptr, bptr, len);    /* BSD doc says bcopy () works right
  1115.                    for overlapping strings.  In ANSI
  1116.                    C, this will be memmove (). */
  1117.     bptr += len;
  1118.     *bptr++ = c;
  1119.     fptr = ++sptr;
  1120.   }
  1121.   len = buf->length - (fptr - buf->buf);
  1122.   if (bptr != fptr && len > 0)
  1123.     bcopy (fptr, bptr, len);
  1124.   buf->length -= fptr - bptr;
  1125.   buf->buf[buf->length] = '\0';
  1126.   if (warn_trigraphs && fptr != bptr)
  1127.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  1128. }
  1129.  
  1130. /* Move all backslash-newline pairs out of embarrassing places.
  1131.    Exchange all such pairs following BP
  1132.    with any potentially-embarrasing characters that follow them.
  1133.    Potentially-embarrassing characters are / and *
  1134.    (because a backslash-newline inside a comment delimiter
  1135.    would cause it not to be recognized).  */
  1136.  
  1137. newline_fix (bp)
  1138.      U_CHAR *bp;
  1139. {
  1140.   register U_CHAR *p = bp;
  1141.   register int count = 0;
  1142.  
  1143.   /* First count the backslash-newline pairs here.  */
  1144.  
  1145.   while (*p++ == '\\' && *p++ == '\n')
  1146.     count++;
  1147.  
  1148.   p = bp + count * 2;
  1149.  
  1150.   /* What follows the backslash-newlines is not embarrassing.  */
  1151.  
  1152.   if (count == 0 || (*p != '/' && *p != '*'))
  1153.     return;
  1154.  
  1155.   /* Copy all potentially embarrassing characters
  1156.      that follow the backslash-newline pairs
  1157.      down to where the pairs originally started.  */
  1158.  
  1159.   while (*p == '*' || *p == '/')
  1160.     *bp++ = *p++;
  1161.  
  1162.   /* Now write the same number of pairs after the embarrassing chars.  */
  1163.   while (count-- > 0) {
  1164.     *bp++ = '\\';
  1165.     *bp++ = '\n';
  1166.   }
  1167. }
  1168.  
  1169. /* Like newline_fix but for use within a directive-name.
  1170.    Move any backslash-newlines up past any following symbol constituents.  */
  1171.  
  1172. name_newline_fix (bp)
  1173.      U_CHAR *bp;
  1174. {
  1175.   register U_CHAR *p = bp;
  1176.   register int count = 0;
  1177.  
  1178.   /* First count the backslash-newline pairs here.  */
  1179.  
  1180.   while (*p++ == '\\' && *p++ == '\n')
  1181.     count++;
  1182.  
  1183.   p = bp + count * 2;
  1184.  
  1185.   /* What follows the backslash-newlines is not embarrassing.  */
  1186.  
  1187.   if (count == 0 || !is_idchar[*p])
  1188.     return;
  1189.  
  1190.   /* Copy all potentially embarrassing characters
  1191.      that follow the backslash-newline pairs
  1192.      down to where the pairs originally started.  */
  1193.  
  1194.   while (is_idchar[*p])
  1195.     *bp++ = *p++;
  1196.  
  1197.   /* Now write the same number of pairs after the embarrassing chars.  */
  1198.   while (count-- > 0) {
  1199.     *bp++ = '\\';
  1200.     *bp++ = '\n';
  1201.   }
  1202. }
  1203.  
  1204. /*
  1205.  * The main loop of the program.
  1206.  *
  1207.  * Read characters from the input stack, transferring them to the
  1208.  * output buffer OP.
  1209.  *
  1210.  * Macros are expanded and push levels on the input stack.
  1211.  * At the end of such a level it is popped off and we keep reading.
  1212.  * At the end of any other kind of level, we return.
  1213.  * #-directives are handled, except within macros.
  1214.  *
  1215.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  1216.  * and insert them when appropriate.  This is set while scanning macro
  1217.  * arguments before substitution.  It is zero when scanning for final output.
  1218.  *   There are three types of Newline markers:
  1219.  *   * Newline -  follows a macro name that was not expanded
  1220.  *     because it appeared inside an expansion of the same macro.
  1221.  *     This marker prevents future expansion of that identifier.
  1222.  *     When the input is rescanned into the final output, these are deleted.
  1223.  *     These are also deleted by ## concatenation.
  1224.  *   * Newline Space (or Newline and any other whitespace character)
  1225.  *     stands for a place that tokens must be separated or whitespace
  1226.  *     is otherwise desirable, but where the ANSI standard specifies there
  1227.  *     is no whitespace.  This marker turns into a Space (or whichever other
  1228.  *     whitespace char appears in the marker) in the final output,
  1229.  *     but it turns into nothing in an argument that is stringified with #.
  1230.  *     Such stringified arguments are the only place where the ANSI standard
  1231.  *     specifies with precision that whitespace may not appear.
  1232.  *
  1233.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  1234.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  1235.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  1236.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  1237.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  1238.  */
  1239.  
  1240. rescan (op, output_marks)
  1241.      FILE_BUF *op;
  1242.      int output_marks;
  1243. {
  1244.   /* Character being scanned in main loop.  */
  1245.   register U_CHAR c;
  1246.  
  1247.   /* Length of pending accumulated identifier.  */
  1248.   register int ident_length = 0;
  1249.  
  1250.   /* Hash code of pending accumulated identifier.  */
  1251.   register int hash = 0;
  1252.  
  1253.   /* Current input level (&instack[indepth]).  */
  1254.   FILE_BUF *ip;
  1255.  
  1256.   /* Pointer for scanning input.  */
  1257.   register U_CHAR *ibp;
  1258.  
  1259.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  1260.   register U_CHAR *limit;
  1261.  
  1262.   /* Pointer for storing output.  */
  1263.   register U_CHAR *obp;
  1264.  
  1265.   /* REDO_CHAR is nonzero if we are processing an identifier
  1266.      after backing up over the terminating character.
  1267.      Sometimes we process an identifier without backing up over
  1268.      the terminating character, if the terminating character
  1269.      is not special.  Backing up is done so that the terminating character
  1270.      will be dispatched on again once the identifier is dealt with.  */
  1271.   int redo_char = 0;
  1272.  
  1273.   /* 1 if within an identifier inside of which a concatenation
  1274.      marker (Newline -) has been seen.  */
  1275.   int concatenated = 0;
  1276.  
  1277.   /* While scanning a comment or a string constant,
  1278.      this records the line it started on, for error messages.  */
  1279.   int start_line;
  1280.  
  1281.   /* Record position of last `real' newline.  */
  1282.   U_CHAR *beg_of_line;
  1283.  
  1284. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  1285.  
  1286. #define POPMACRO \
  1287. do { ip->macro->type = T_MACRO;        \
  1288.      if (ip->free_ptr) free (ip->free_ptr);    \
  1289.      --indepth; } while (0)
  1290.  
  1291. /* Reload `rescan's local variables that describe the current
  1292.    level of the input stack.  */
  1293.  
  1294. #define RECACHE  \
  1295. do { ip = &instack[indepth];        \
  1296.      ibp = ip->bufp;            \
  1297.      limit = ip->buf + ip->length;    \
  1298.      op->bufp = obp;            \
  1299.      check_expand (op, limit - ibp);    \
  1300.      beg_of_line = 0;            \
  1301.      obp = op->bufp; } while (0)
  1302.  
  1303.   if (no_output && instack[indepth].fname != 0)
  1304.     skip_if_group (&instack[indepth], 1);
  1305.  
  1306.   obp = op->bufp;
  1307.   RECACHE;
  1308.   beg_of_line = ibp;
  1309.  
  1310.   /* Our caller must always put a null after the end of
  1311.      the input at each input stack level.  */
  1312.   if (*limit != 0)
  1313.     abort ();
  1314.  
  1315.   while (1) {
  1316.     c = *ibp++;
  1317.     *obp++ = c;
  1318.  
  1319.     switch (c) {
  1320.     case '\\':
  1321.       if (ibp >= limit)
  1322.     break;
  1323.       if (*ibp == '\n') {
  1324.     /* Always merge lines ending with backslash-newline,
  1325.        even in middle of identifier.  */
  1326.     ++ibp;
  1327.     ++ip->lineno;
  1328.     --obp;        /* remove backslash from obuf */
  1329.     break;
  1330.       }
  1331.       /* Otherwise, backslash suppresses specialness of following char,
  1332.      so copy it here to prevent the switch from seeing it.
  1333.      But first get any pending identifier processed.  */
  1334.       if (ident_length > 0)
  1335.     goto specialchar;
  1336.       *obp++ = *ibp++;
  1337.       break;
  1338.  
  1339.     case '#':
  1340.       /* If this is expanding a macro definition, don't recognize
  1341.      preprocessor directives.  */
  1342.       if (ip->macro != 0)
  1343.     goto randomchar;
  1344.       if (ident_length)
  1345.     goto specialchar;
  1346.  
  1347.       /* # keyword: a # must be first nonblank char on the line */
  1348.       if (beg_of_line == 0)
  1349.     goto randomchar;
  1350.       {
  1351.     U_CHAR *bp;
  1352.  
  1353.     /* Scan from start of line, skipping whitespace, comments
  1354.        and backslash-newlines, and see if we reach this #.
  1355.        If not, this # is not special.  */
  1356.     bp = beg_of_line;
  1357.     while (1) {
  1358.       if (is_hor_space[*bp])
  1359.         bp++;
  1360.       else if (*bp == '\\' && bp[1] == '\n')
  1361.         bp += 2;
  1362.       else if (*bp == '/' && bp[1] == '*') {
  1363.         bp += 2;
  1364.         while (!(*bp == '*' && bp[1] == '/'))
  1365.           bp++;
  1366.         bp += 2;
  1367.       }
  1368.       else if (cplusplus && *bp == '/' && bp[1] == '/') {
  1369.         bp += 2;
  1370.         while (*bp++ != '\n') ;
  1371.       }
  1372.       else break;
  1373.     }
  1374.     if (bp + 1 != ibp)
  1375.       goto randomchar;
  1376.       }
  1377.  
  1378.       /* This # can start a directive.  */
  1379.  
  1380.       --obp;        /* Don't copy the '#' */
  1381.  
  1382.       ip->bufp = ibp;
  1383.       op->bufp = obp;
  1384.       if (! handle_directive (ip, op)) {
  1385. #ifdef USE_C_ALLOCA
  1386.     alloca (0);
  1387. #endif
  1388.     /* Not a known directive: treat it as ordinary text.
  1389.        IP, OP, IBP, etc. have not been changed.  */
  1390.     if (no_output && instack[indepth].fname) {
  1391.       /* If not generating expanded output,
  1392.          what we do with ordinary text is skip it.
  1393.          Discard everything until next # directive.  */
  1394.       skip_if_group (&instack[indepth], 1);
  1395.       RECACHE;
  1396.       beg_of_line = ibp;
  1397.       break;
  1398.     }
  1399.     ++obp;        /* Copy the '#' after all */
  1400.     goto randomchar;
  1401.       }
  1402. #ifdef USE_C_ALLOCA
  1403.       alloca (0);
  1404. #endif
  1405.       /* A # directive has been successfully processed.  */
  1406.       /* If not generating expanded output, ignore everything until
  1407.      next # directive.  */
  1408.       if (no_output && instack[indepth].fname)
  1409.     skip_if_group (&instack[indepth], 1);
  1410.       obp = op->bufp;
  1411.       RECACHE;
  1412.       beg_of_line = ibp;
  1413.       break;
  1414.  
  1415.     case '\"':            /* skip quoted string */
  1416.     case '\'':
  1417.       /* A single quoted string is treated like a double -- some
  1418.      programs (e.g., troff) are perverse this way */
  1419.  
  1420.       if (ident_length)
  1421.     goto specialchar;
  1422.  
  1423.       start_line = ip->lineno;
  1424.  
  1425.       /* Skip ahead to a matching quote.  */
  1426.  
  1427.       while (1) {
  1428.     if (ibp >= limit) {
  1429.       if (traditional) {
  1430.         if (ip->macro != 0) {
  1431.           /* try harder: this string crosses a macro expansion boundary */
  1432.           POPMACRO;
  1433.           RECACHE;
  1434.           continue;
  1435.         }
  1436.       } else
  1437.         error_with_line (line_for_error (start_line),
  1438.                  "unterminated string or character constant");
  1439.       break;
  1440.     }
  1441.     *obp++ = *ibp;
  1442.     switch (*ibp++) {
  1443.     case '\n':
  1444.       ++ip->lineno;
  1445.       ++op->lineno;
  1446.       /* Traditionally, end of line ends a string constant with no error.
  1447.          So exit the loop and record the new line.  */
  1448.       if (traditional) {
  1449.         beg_of_line = ibp;
  1450.         goto while2end;
  1451.       }
  1452.       if (pedantic || c == '\'') {
  1453.         error_with_line (line_for_error (start_line),
  1454.                  "unterminated string or character constant");
  1455.         goto while2end;
  1456.       }
  1457.       break;
  1458.  
  1459.     case '\\':
  1460.       if (ibp >= limit)
  1461.         break;
  1462.       if (*ibp == '\n') {
  1463.         /* Backslash newline is replaced by nothing at all,
  1464.            but keep the line counts correct.  */
  1465.         --obp;
  1466.         ++ibp;
  1467.         ++ip->lineno;
  1468.       } else {
  1469.         /* ANSI stupidly requires that in \\ the second \
  1470.            is *not* prevented from combining with a newline.  */
  1471.         while (*ibp == '\\' && ibp[1] == '\n') {
  1472.           ibp += 2;
  1473.           ++ip->lineno;
  1474.         }
  1475.         *obp++ = *ibp++;
  1476.       }
  1477.       break;
  1478.  
  1479.     case '\"':
  1480.     case '\'':
  1481.       if (ibp[-1] == c)
  1482.         goto while2end;
  1483.       break;
  1484.     }
  1485.       }
  1486.     while2end:
  1487.       break;
  1488.  
  1489.     case '/':
  1490.       if (*ibp == '\\' && ibp[1] == '\n')
  1491.     newline_fix (ibp);
  1492.       if (cplusplus && *ibp == '/') {
  1493.     /* C++ style comment... */
  1494.     start_line = ip->lineno;
  1495.  
  1496.     --ibp;            /* Back over the slash */
  1497.     --obp;
  1498.  
  1499.     /* Comments are equivalent to spaces. */
  1500.     if (! put_out_comments)
  1501.       *obp++ = ' ';
  1502.     else {
  1503.       /* must fake up a comment here */
  1504.       *obp++ = '/';
  1505.       *obp++ = '/';
  1506.     }
  1507.     {
  1508.       U_CHAR *before_bp = ibp+2;
  1509.  
  1510.       while (ibp < limit) {
  1511.         if (*ibp++ == '\n') {
  1512.           ibp--;
  1513.           if (put_out_comments) {
  1514.         bcopy (before_bp, obp, ibp - before_bp);
  1515.         obp += ibp - before_bp;
  1516.           }
  1517.           break;
  1518.         }
  1519.       }
  1520.       break;
  1521.     }
  1522.       }
  1523.       if (*ibp != '*')
  1524.     goto randomchar;
  1525.       if (ip->macro != 0)
  1526.     goto randomchar;
  1527.       if (ident_length)
  1528.     goto specialchar;
  1529.  
  1530.       /* We have a comment.  Skip it, optionally copying it to output.  */
  1531.  
  1532.       start_line = ip->lineno;
  1533.  
  1534.       ++ibp;            /* Skip the star. */
  1535.  
  1536.       /* Comments are equivalent to spaces.
  1537.      Note that we already output the slash; we might not want it.
  1538.      For -traditional, a comment is equivalent to nothing.  */
  1539.       if (! put_out_comments) {
  1540.     if (traditional)
  1541.       obp--;
  1542.     else
  1543.       obp[-1] = ' ';
  1544.       }
  1545.       else
  1546.     *obp++ = '*';
  1547.  
  1548.       {
  1549.     U_CHAR *before_bp = ibp;
  1550.  
  1551.     while (ibp < limit) {
  1552.       switch (*ibp++) {
  1553.       case '/':
  1554.         if (warn_comments && ibp < limit && *ibp == '*')
  1555.           warning("`/*' within comment");
  1556.         break;
  1557.       case '*':
  1558.         if (*ibp == '\\' && ibp[1] == '\n')
  1559.           newline_fix (ibp);
  1560.         if (ibp >= limit || *ibp == '/')
  1561.           goto comment_end;
  1562.         break;
  1563.       case '\n':
  1564.         ++ip->lineno;
  1565.         /* Copy the newline into the output buffer, in order to
  1566.            avoid the pain of a #line every time a multiline comment
  1567.            is seen.  */
  1568.         if (!put_out_comments)
  1569.           *obp++ = '\n';
  1570.         ++op->lineno;
  1571.       }
  1572.     }
  1573.       comment_end:
  1574.  
  1575.     if (ibp >= limit)
  1576.       error_with_line (line_for_error (start_line),
  1577.                "unterminated comment");
  1578.     else {
  1579.       ibp++;
  1580.       if (put_out_comments) {
  1581.         bcopy (before_bp, obp, ibp - before_bp);
  1582.         obp += ibp - before_bp;
  1583.       }
  1584.     }
  1585.       }
  1586.       break;
  1587.  
  1588.     case '$':
  1589.       if (!dollars_in_ident)
  1590.     goto randomchar;
  1591.       goto letter;
  1592.  
  1593.     case '0': case '1': case '2': case '3': case '4':
  1594.     case '5': case '6': case '7': case '8': case '9':
  1595.       /* If digit is not part of identifier, it starts a number,
  1596.      which means that following letters are not an identifier.
  1597.      "0x5" does not refer to an identifier "x5".
  1598.      So copy all alphanumerics that follow without accumulating
  1599.      as an identifier.  Periods also, for sake of "3.e7".  */
  1600.  
  1601.       if (ident_length == 0) {
  1602.     while (ibp < limit) {
  1603.       c = *ibp++;
  1604.       if (!isalnum (c) && c != '.' && c != '_') {
  1605.         --ibp;
  1606.         break;
  1607.       }
  1608.       *obp++ = c;
  1609.       /* A sign can be part of a preprocessing number
  1610.          if it follows an e.  */
  1611.       if (c == 'e' || c == 'E') {
  1612.         if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
  1613.           *obp++ = *ibp++;
  1614.           /* But traditional C does not let the token go past the sign.  */
  1615.           if (traditional)
  1616.         break;
  1617.         }
  1618.       }
  1619.     }
  1620.     break;
  1621.       }
  1622.       /* fall through */
  1623.  
  1624.     case '_':
  1625.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  1626.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  1627.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  1628.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  1629.     case 'y': case 'z':
  1630.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  1631.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  1632.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  1633.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  1634.     case 'Y': case 'Z':
  1635.     letter:
  1636.       ident_length++;
  1637.       /* Compute step of hash function, to avoid a proc call on every token */
  1638.       hash = HASHSTEP (hash, c);
  1639.       break;
  1640.  
  1641.     case '\n':
  1642.       /* If reprocessing a macro expansion, newline is a special marker.  */
  1643.       if (ip->macro != 0) {
  1644.     /* Newline White is a "funny space" to separate tokens that are
  1645.        supposed to be separate but without space between.
  1646.        Here White means any horizontal whitespace character.
  1647.        Newline - marks a recursive macro use that is not
  1648.        supposed to be expandable.  */
  1649.  
  1650.     if (*ibp == '-') {
  1651.       /* Newline - inhibits expansion of preceding token.
  1652.          If expanding a macro arg, we keep the newline -.
  1653.          In final output, it is deleted.  */
  1654.       if (! concatenated) {
  1655.         ident_length = 0;
  1656.         hash = 0;
  1657.       }
  1658.       ibp++;
  1659.       if (!output_marks) {
  1660.         obp--;
  1661.       } else {
  1662.         /* If expanding a macro arg, keep the newline -.  */
  1663.         *obp++ = '-';
  1664.       }
  1665.     } else if (is_space[*ibp]) {
  1666.       /* Newline Space does not prevent expansion of preceding token
  1667.          so expand the preceding token and then come back.  */
  1668.       if (ident_length > 0)
  1669.         goto specialchar;
  1670.  
  1671.       /* If generating final output, newline space makes a space.  */
  1672.       if (!output_marks) {
  1673.         obp[-1] = *ibp++;
  1674.         /* And Newline Newline makes a newline, so count it.  */
  1675.         if (obp[-1] == '\n')
  1676.           op->lineno++;
  1677.       } else {
  1678.         /* If expanding a macro arg, keep the newline space.
  1679.            If the arg gets stringified, newline space makes nothing.  */
  1680.         *obp++ = *ibp++;
  1681.       }
  1682.     } else abort ();    /* Newline followed by something random?  */
  1683.     break;
  1684.       }
  1685.  
  1686.       /* If there is a pending identifier, handle it and come back here.  */
  1687.       if (ident_length > 0)
  1688.     goto specialchar;
  1689.  
  1690.       beg_of_line = ibp;
  1691.  
  1692.       /* Update the line counts and output a #line if necessary.  */
  1693.       ++ip->lineno;
  1694.       ++op->lineno;
  1695.       if (ip->lineno != op->lineno) {
  1696.     op->bufp = obp;
  1697.     output_line_command (ip, op, 1, same_file);
  1698.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  1699.     obp = op->bufp;
  1700.       }
  1701.       break;
  1702.  
  1703.       /* Come here either after (1) a null character that is part of the input
  1704.      or (2) at the end of the input, because there is a null there.  */
  1705.     case 0:
  1706.       if (ibp <= limit)
  1707.     /* Our input really contains a null character.  */
  1708.     goto randomchar;
  1709.  
  1710.       /* At end of a macro-expansion level, pop it and read next level.  */
  1711.       if (ip->macro != 0) {
  1712.     obp--;
  1713.     ibp--;
  1714.     /* If traditional, and we have an identifier that ends here,
  1715.        process it now, so we get the right error for recursion.  */
  1716.     if (traditional && ident_length
  1717.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  1718.       redo_char = 1;
  1719.       goto randomchar;
  1720.     }
  1721.     POPMACRO;
  1722.     RECACHE;
  1723.     break;
  1724.       }
  1725.  
  1726.       /* If we don't have a pending identifier,
  1727.      return at end of input.  */
  1728.       if (ident_length == 0) {
  1729.     obp--;
  1730.     ibp--;
  1731.     op->bufp = obp;
  1732.     ip->bufp = ibp;
  1733.     goto ending;
  1734.       }
  1735.  
  1736.       /* If we do have a pending identifier, just consider this null
  1737.      a special character and arrange to dispatch on it again.
  1738.      The second time, IDENT_LENGTH will be zero so we will return.  */
  1739.  
  1740.       /* Fall through */
  1741.  
  1742. specialchar:
  1743.  
  1744.       /* Handle the case of a character such as /, ', " or null
  1745.      seen following an identifier.  Back over it so that
  1746.      after the identifier is processed the special char
  1747.      will be dispatched on again.  */
  1748.  
  1749.       ibp--;
  1750.       obp--;
  1751.       redo_char = 1;
  1752.  
  1753.     default:
  1754.  
  1755. randomchar:
  1756.  
  1757.       if (ident_length > 0) {
  1758.     register HASHNODE *hp;
  1759.  
  1760.     /* We have just seen an identifier end.  If it's a macro, expand it.
  1761.  
  1762.        IDENT_LENGTH is the length of the identifier
  1763.        and HASH is its hash code.
  1764.  
  1765.        The identifier has already been copied to the output,
  1766.        so if it is a macro we must remove it.
  1767.  
  1768.        If REDO_CHAR is 0, the char that terminated the identifier
  1769.        has been skipped in the output and the input.
  1770.        OBP-IDENT_LENGTH-1 points to the identifier.
  1771.        If the identifier is a macro, we must back over the terminator.
  1772.  
  1773.        If REDO_CHAR is 1, the terminating char has already been
  1774.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  1775.  
  1776.     for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  1777.          hp = hp->next) {
  1778.  
  1779.       if (hp->length == ident_length) {
  1780.         U_CHAR *obufp_before_macroname;
  1781.         int op_lineno_before_macroname;
  1782.         register int i = ident_length;
  1783.         register U_CHAR *p = hp->name;
  1784.         register U_CHAR *q = obp - i;
  1785.         int disabled;
  1786.  
  1787.         if (! redo_char)
  1788.           q--;
  1789.  
  1790.         do {        /* All this to avoid a strncmp () */
  1791.           if (*p++ != *q++)
  1792.         goto hashcollision;
  1793.         } while (--i);
  1794.  
  1795.         /* We found a use of a macro name.
  1796.            see if the context shows it is a macro call.  */
  1797.  
  1798.         /* Back up over terminating character if not already done.  */
  1799.         if (! redo_char) {
  1800.           ibp--;
  1801.           obp--;
  1802.         }
  1803.  
  1804.         obufp_before_macroname = obp - ident_length;
  1805.         op_lineno_before_macroname = op->lineno;
  1806.  
  1807.         /* Record whether the macro is disabled.  */
  1808.         disabled = hp->type == T_DISABLED;
  1809.  
  1810.         /* This looks like a macro ref, but if the macro was disabled,
  1811.            just copy its name and put in a marker if requested.  */
  1812.  
  1813.         if (disabled) {
  1814. #if 0
  1815.           /* This error check caught useful cases such as
  1816.          #define foo(x,y) bar(x(y,0), y)
  1817.          foo(foo, baz)  */
  1818.           if (traditional)
  1819.         error ("recursive use of macro `%s'", hp->name);
  1820. #endif
  1821.  
  1822.           if (output_marks) {
  1823.         check_expand (op, limit - ibp + 2);
  1824.         *obp++ = '\n';
  1825.         *obp++ = '-';
  1826.           }
  1827.           break;
  1828.         }
  1829.  
  1830.         /* If macro wants an arglist, verify that a '(' follows.
  1831.            first skip all whitespace, copying it to the output
  1832.            after the macro name.  Then, if there is no '(',
  1833.            decide this is not a macro call and leave things that way.  */
  1834.         if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  1835.         && hp->value.defn->nargs >= 0)
  1836.           {
  1837.         while (1) {
  1838.           /* Scan forward over whitespace, copying it to the output.  */
  1839.           if (ibp == limit && ip->macro != 0) {
  1840.             POPMACRO;
  1841.             RECACHE;
  1842.           }
  1843.           /* A comment: copy it unchanged or discard it.  */
  1844.           else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  1845.             if (put_out_comments) {
  1846.               *obp++ = '/';
  1847.               *obp++ = '*';
  1848.             } else if (! traditional) {
  1849.               *obp++ = ' ';
  1850.             }
  1851.             ibp += 2;
  1852.             while (ibp + 1 != limit
  1853.                && !(ibp[0] == '*' && ibp[1] == '/')) {
  1854.               /* We need not worry about newline-marks,
  1855.              since they are never found in comments.  */
  1856.               if (*ibp == '\n') {
  1857.             /* Newline in a file.  Count it.  */
  1858.             ++ip->lineno;
  1859.             ++op->lineno;
  1860.               }
  1861.               if (put_out_comments)
  1862.             *obp++ = *ibp++;
  1863.               else
  1864.             ibp++;
  1865.             }
  1866.             ibp += 2;
  1867.             if (put_out_comments) {
  1868.               *obp++ = '*';
  1869.               *obp++ = '/';
  1870.             }
  1871.           }
  1872.           else if (is_space[*ibp]) {
  1873.             *obp++ = *ibp++;
  1874.             if (ibp[-1] == '\n') {
  1875.               if (ip->macro == 0) {
  1876.             /* Newline in a file.  Count it.  */
  1877.             ++ip->lineno;
  1878.             ++op->lineno;
  1879.               } else if (!output_marks) {
  1880.             /* A newline mark, and we don't want marks
  1881.                in the output.  If it is newline-hyphen,
  1882.                discard it entirely.  Otherwise, it is
  1883.                newline-whitechar, so keep the whitechar.  */
  1884.             obp--;
  1885.             if (*ibp == '-')
  1886.               ibp++;
  1887.             else {
  1888.               if (*ibp == '\n')
  1889.                 ++op->lineno;
  1890.               *obp++ = *ibp++;
  1891.             }
  1892.               } else {
  1893.             /* A newline mark; copy both chars to the output.  */
  1894.             *obp++ = *ibp++;
  1895.               }
  1896.             }
  1897.           }
  1898.           else break;
  1899.         }
  1900.         if (*ibp != '(')
  1901.           break;
  1902.           }
  1903.  
  1904.         /* This is now known to be a macro call.
  1905.            Discard the macro name from the output,
  1906.            along with any following whitespace just copied.  */
  1907.         obp = obufp_before_macroname;
  1908.         op->lineno = op_lineno_before_macroname;
  1909.  
  1910.         /* Expand the macro, reading arguments as needed,
  1911.            and push the expansion on the input stack.  */
  1912.         ip->bufp = ibp;
  1913.         op->bufp = obp;
  1914.         macroexpand (hp, op);
  1915.  
  1916.         /* Reexamine input stack, since macroexpand has pushed
  1917.            a new level on it.  */
  1918.         obp = op->bufp;
  1919.         RECACHE;
  1920.         break;
  1921.       }
  1922. hashcollision:
  1923.            ;
  1924.     }            /* End hash-table-search loop */
  1925.     ident_length = hash = 0; /* Stop collecting identifier */
  1926.     redo_char = 0;
  1927.     concatenated = 0;
  1928.       }                /* End if (ident_length > 0) */
  1929.     }                /* End switch */
  1930.   }                /* End per-char loop */
  1931.  
  1932.   /* Come here to return -- but first give an error message
  1933.      if there was an unterminated successful conditional.  */
  1934.  ending:
  1935.   if (if_stack != ip->if_stack) {
  1936.     char *str;
  1937.     switch (if_stack->type) {
  1938.     case T_IF:
  1939.       str = "if";
  1940.       break;
  1941.     case T_IFDEF:
  1942.       str = "ifdef";
  1943.       break;
  1944.     case T_IFNDEF:
  1945.       str = "ifndef";
  1946.       break;
  1947.     case T_ELSE:
  1948.       str = "else";
  1949.       break;
  1950.     case T_ELIF:
  1951.       str = "elif";
  1952.       break;
  1953.     }
  1954.     error_with_line (line_for_error (if_stack->lineno),
  1955.              "unterminated #%s conditional", str);
  1956.   }
  1957.   if_stack = ip->if_stack;
  1958. }
  1959.  
  1960. /*
  1961.  * Rescan a string into a temporary buffer and return the result
  1962.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  1963.  *
  1964.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  1965.  * and insert such markers when appropriate.  See `rescan' for details.
  1966.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  1967.  * before substitution; it is 0 for other uses.
  1968.  */
  1969. FILE_BUF
  1970. expand_to_temp_buffer (buf, limit, output_marks)
  1971.      U_CHAR *buf, *limit;
  1972.      int output_marks;
  1973. {
  1974.   register FILE_BUF *ip;
  1975.   FILE_BUF obuf;
  1976.   int length = limit - buf;
  1977.   U_CHAR *buf1;
  1978.   int odepth = indepth;
  1979.  
  1980.   if (length < 0)
  1981.     abort ();
  1982.  
  1983.   /* Set up the input on the input stack.  */
  1984.  
  1985.   buf1 = (U_CHAR *) alloca (length + 1);
  1986.   {
  1987.     register U_CHAR *p1 = buf;
  1988.     register U_CHAR *p2 = buf1;
  1989.  
  1990.     while (p1 != limit)
  1991.       *p2++ = *p1++;
  1992.   }
  1993.   buf1[length] = 0;
  1994.  
  1995.   /* Set up to receive the output.  */
  1996.  
  1997.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  1998.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  1999.   obuf.fname = 0;
  2000.   obuf.macro = 0;
  2001.   obuf.free_ptr = 0;
  2002.  
  2003.   CHECK_DEPTH ({return obuf;});
  2004.  
  2005.   ++indepth;
  2006.  
  2007.   ip = &instack[indepth];
  2008.   ip->fname = 0;
  2009.   ip->macro = 0;
  2010.   ip->free_ptr = 0;
  2011.   ip->length = length;
  2012.   ip->buf = ip->bufp = buf1;
  2013.   ip->if_stack = if_stack;
  2014.  
  2015.   ip->lineno = obuf.lineno = 1;
  2016.  
  2017.   /* Scan the input, create the output.  */
  2018.  
  2019.   rescan (&obuf, output_marks);
  2020.  
  2021.   /* Pop input stack to original state.  */
  2022.   --indepth;
  2023.  
  2024.   if (indepth != odepth)
  2025.     abort ();
  2026.  
  2027.   /* Record the output.  */
  2028.   obuf.length = obuf.bufp - obuf.buf;
  2029.  
  2030.   return obuf;
  2031. }
  2032.  
  2033. /*
  2034.  * Process a # directive.  Expects IP->bufp to point to the '#', as in
  2035.  * `#define foo bar'.  Passes to the command handler
  2036.  * (do_define, do_include, etc.): the addresses of the 1st and
  2037.  * last chars of the command (starting immediately after the #
  2038.  * keyword), plus op and the keyword table pointer.  If the command
  2039.  * contains comments it is copied into a temporary buffer sans comments
  2040.  * and the temporary buffer is passed to the command handler instead.
  2041.  * Likewise for backslash-newlines.
  2042.  *
  2043.  * Returns nonzero if this was a known # directive.
  2044.  * Otherwise, returns zero, without advancing the input pointer.
  2045.  */
  2046.  
  2047. int
  2048. handle_directive (ip, op)
  2049.      FILE_BUF *ip, *op;
  2050. {
  2051.   register U_CHAR *bp, *cp;
  2052.   register struct directive *kt;
  2053.   register int ident_length;
  2054.   U_CHAR *resume_p;
  2055.  
  2056.   /* Nonzero means we must copy the entire command
  2057.      to get rid of comments or backslash-newlines.  */
  2058.   int copy_command = 0;
  2059.  
  2060.   U_CHAR *ident, *after_ident;
  2061.  
  2062.   bp = ip->bufp;
  2063.   /* Skip whitespace and \-newline.  */
  2064.   while (1) {
  2065.     if (is_hor_space[*bp])
  2066.       bp++;
  2067.     else if (*bp == '/' && bp[1] == '*') {
  2068.       ip->bufp = bp;
  2069.       skip_to_end_of_comment (ip, &ip->lineno);
  2070.       bp = ip->bufp;
  2071.     } else if (*bp == '\\' && bp[1] == '\n') {
  2072.       bp += 2; ip->lineno++;
  2073.     } else break;
  2074.   }
  2075.  
  2076.   /* Now find end of directive name.
  2077.      If we encounter a backslash-newline, exchange it with any following
  2078.      symbol-constituents so that we end up with a contiguous name.  */
  2079.  
  2080.   cp = bp;
  2081.   while (1) {
  2082.     if (is_idchar[*cp])
  2083.       cp++;
  2084.     else {
  2085.       if (*cp == '\\' && cp[1] == '\n')
  2086.     name_newline_fix (cp);
  2087.       if (is_idchar[*cp])
  2088.     cp++;
  2089.       else break;
  2090.     }
  2091.   }
  2092.   ident_length = cp - bp;
  2093.   ident = bp;
  2094.   after_ident = cp;
  2095.  
  2096.   /* A line of just `#' becomes blank.  */
  2097.  
  2098.   if (traditional && ident_length == 0 && *after_ident == '\n') {
  2099.     ip->bufp = after_ident;
  2100.     return 1;
  2101.   }
  2102.  
  2103.   /*
  2104.    * Decode the keyword and call the appropriate expansion
  2105.    * routine, after moving the input pointer up to the next line.
  2106.    */
  2107.   for (kt = directive_table; kt->length > 0; kt++) {
  2108.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  2109.       register U_CHAR *buf;
  2110.       register U_CHAR *limit = ip->buf + ip->length;
  2111.       int unterminated = 0;
  2112.  
  2113.       /* Nonzero means do not delete comments within the directive.
  2114.      #define needs this when -traditional.  */
  2115.       int keep_comments = traditional && kt->traditional_comments;
  2116.  
  2117.       /* Find the end of this command (first newline not backslashed
  2118.      and not in a string or comment).
  2119.      Set COPY_COMMAND if the command must be copied
  2120.      (it contains a backslash-newline or a comment).  */
  2121.  
  2122.       buf = bp = after_ident;
  2123.       while (bp < limit) {
  2124.     register U_CHAR c = *bp++;
  2125.     switch (c) {
  2126.     case '\\':
  2127.       if (bp < limit) {
  2128.         if (*bp == '\n') {
  2129.           ip->lineno++;
  2130.           copy_command = 1;
  2131.         }
  2132.         bp++;
  2133.       }
  2134.       break;
  2135.  
  2136.     case '\'':
  2137.     case '\"':
  2138.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  2139.       /* Don't bother calling the directive if we already got an error
  2140.          message due to unterminated string.  Skip everything and pretend
  2141.          we called the directive.  */
  2142.       if (unterminated) {
  2143.         if (traditional) {
  2144.           /* Traditional preprocessing permits unterminated strings.  */
  2145.           ip->bufp = bp;
  2146.           goto endloop1;
  2147.         }
  2148.         ip->bufp = bp;
  2149.         return 1;
  2150.       }
  2151.       break;
  2152.  
  2153.       /* <...> is special for #include.  */
  2154.     case '<':
  2155.       if (!kt->angle_brackets)
  2156.         break;
  2157.       while (*bp && *bp != '>') bp++;
  2158.       break;
  2159.  
  2160.     case '/':
  2161.       if (*bp == '\\' && bp[1] == '\n')
  2162.         newline_fix (bp);
  2163.       if (*bp == '*'
  2164.           || (cplusplus && *bp == '/')) {
  2165.         U_CHAR *obp = bp - 1;
  2166.         ip->bufp = bp + 1;
  2167.         skip_to_end_of_comment (ip, &ip->lineno);
  2168.         bp = ip->bufp;
  2169.         /* No need to copy the command because of a comment at the end;
  2170.            just don't include the comment in the directive.  */
  2171.         if (bp == limit || *bp == '\n') {
  2172.           bp = obp;
  2173.           goto endloop1;
  2174.         }
  2175.         /* Don't remove the comments if -traditional.  */
  2176.         if (! keep_comments)
  2177.           copy_command++;
  2178.       }
  2179.       break;
  2180.  
  2181.     case '\n':
  2182.       --bp;        /* Point to the newline */
  2183.       ip->bufp = bp;
  2184.       goto endloop1;
  2185.     }
  2186.       }
  2187.       ip->bufp = bp;
  2188.  
  2189.     endloop1:
  2190.       resume_p = ip->bufp;
  2191.       /* BP is the end of the directive.
  2192.      RESUME_P is the next interesting data after the directive.
  2193.      A comment may come between.  */
  2194.  
  2195.       if (copy_command) {
  2196.     register U_CHAR *xp = buf;
  2197.     /* Need to copy entire command into temp buffer before dispatching */
  2198.  
  2199.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  2200.                           some slop */
  2201.     buf = cp;
  2202.  
  2203.     /* Copy to the new buffer, deleting comments
  2204.        and backslash-newlines (and whitespace surrounding the latter).  */
  2205.  
  2206.     while (xp < bp) {
  2207.       register U_CHAR c = *xp++;
  2208.       *cp++ = c;
  2209.  
  2210.       switch (c) {
  2211.       case '\n':
  2212.         break;
  2213.  
  2214.         /* <...> is special for #include.  */
  2215.       case '<':
  2216.         if (!kt->angle_brackets)
  2217.           break;
  2218.         while (xp < bp && c != '>') {
  2219.           c = *xp++;
  2220.           *cp++ = c;
  2221.         }
  2222.         break;
  2223.  
  2224.       case '\\':
  2225.         if (*xp == '\n') {
  2226.           xp++;
  2227.           cp--;
  2228.           if (cp != buf && is_space[cp[-1]]) {
  2229.         while (cp != buf && is_space[cp[-1]]) cp--;
  2230.         cp++;
  2231.         SKIP_WHITE_SPACE (xp);
  2232.           } else if (is_space[*xp]) {
  2233.         *cp++ = *xp++;
  2234.         SKIP_WHITE_SPACE (xp);
  2235.           }
  2236.         }
  2237.         break;
  2238.  
  2239.       case '\'':
  2240.       case '\"':
  2241.         {
  2242.           register U_CHAR *bp1
  2243.         = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
  2244.           while (xp != bp1)
  2245.         *cp++ = *xp++;
  2246.         }
  2247.         break;
  2248.  
  2249.       case '/':
  2250.         if (*xp == '*'
  2251.         || (cplusplus && *xp == '/')) {
  2252.           ip->bufp = xp + 1;
  2253.           skip_to_end_of_comment (ip, 0);
  2254.           if (keep_comments)
  2255.         while (xp != ip->bufp)
  2256.           *cp++ = *xp++;
  2257.           /* Delete or replace the slash.  */
  2258.           else if (traditional)
  2259.         cp--;
  2260.           else
  2261.         cp[-1] = ' ';
  2262.           xp = ip->bufp;
  2263.         }
  2264.       }
  2265.     }
  2266.  
  2267.     /* Null-terminate the copy.  */
  2268.  
  2269.     *cp = 0;
  2270.       }
  2271.       else
  2272.     cp = bp;
  2273.  
  2274.       ip->bufp = resume_p;
  2275.  
  2276.       /* Some directives should be written out for cc1 to process,
  2277.      just as if they were not defined.  */
  2278.  
  2279.       if (kt->pass_thru) {
  2280.         int len;
  2281.  
  2282.     /* Output directive name.  */
  2283.         check_expand (op, kt->length+1);
  2284.         *op->bufp++ = '#';
  2285.         bcopy (kt->name, op->bufp, kt->length);
  2286.         op->bufp += kt->length;
  2287.  
  2288.     /* Output arguments.  */
  2289.         len = (cp - buf);
  2290.         check_expand (op, len);
  2291.         bcopy (buf, op->bufp, len);
  2292.         op->bufp += len;
  2293.       }
  2294.  
  2295.       /* Call the appropriate command handler.  buf now points to
  2296.      either the appropriate place in the input buffer, or to
  2297.      the temp buffer if it was necessary to make one.  cp
  2298.      points to the first char after the contents of the (possibly
  2299.      copied) command, in either case. */
  2300.       (*kt->func) (buf, cp, op, kt);
  2301.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  2302.  
  2303.       return 1;
  2304.     }
  2305.   }
  2306.  
  2307.   return 0;
  2308. }
  2309.  
  2310. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2311.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  2312.                 };
  2313.  
  2314. /*
  2315.  * expand things like __FILE__.  Place the expansion into the output
  2316.  * buffer *without* rescanning.
  2317.  */
  2318. special_symbol (hp, op)
  2319.      HASHNODE *hp;
  2320.      FILE_BUF *op;
  2321. {
  2322.   char *buf;
  2323.   long t;
  2324.   int i, len;
  2325.   int true_indepth;
  2326.   FILE_BUF *ip = NULL;
  2327.   static struct tm *timebuf = NULL;
  2328.   struct tm *localtime ();
  2329.  
  2330.   int paren = 0;        /* For special `defined' keyword */
  2331.  
  2332.   for (i = indepth; i >= 0; i--)
  2333.     if (instack[i].fname != NULL) {
  2334.       ip = &instack[i];
  2335.       break;
  2336.     }
  2337.   if (ip == NULL) {
  2338.     error ("cccp error: not in any file?!");
  2339.     return;            /* the show must go on */
  2340.   }
  2341.  
  2342.   switch (hp->type) {
  2343.   case T_FILE:
  2344.   case T_BASE_FILE:
  2345.     {
  2346.       char *string;
  2347.       if (hp->type == T_FILE)
  2348.     string = ip->fname;
  2349.       else
  2350.     string = instack[0].fname;
  2351.  
  2352.       if (string)
  2353.     {
  2354.       buf = (char *) alloca (3 + strlen (string));
  2355.       sprintf (buf, "\"%s\"", string);
  2356.     }
  2357.       else
  2358.     buf = "\"\"";
  2359.  
  2360.       break;
  2361.     }
  2362.  
  2363.   case T_INCLUDE_LEVEL:
  2364.     true_indepth = 0;
  2365.     for (i = indepth; i >= 0; i--)
  2366.       if (instack[i].fname != NULL)
  2367.         true_indepth++;
  2368.  
  2369.     buf = (char *) alloca (8);    /* Eigth bytes ought to be more than enough */
  2370.     sprintf (buf, "%d", true_indepth - 1);
  2371.     break;
  2372.  
  2373.   case T_VERSION:
  2374.     buf = (char *) alloca (3 + strlen (version_string));
  2375.     sprintf (buf, "\"%s\"", version_string);
  2376.     break;
  2377.  
  2378.   case T_CONST:
  2379.     buf = (char *) alloca (4 * sizeof (int));
  2380.     sprintf (buf, "%d", hp->value.ival);
  2381.     break;
  2382.  
  2383.   case T_SPECLINE:
  2384.     buf = (char *) alloca (10);
  2385.     sprintf (buf, "%d", ip->lineno);
  2386.     break;
  2387.  
  2388.   case T_DATE:
  2389.   case T_TIME:
  2390.     if (timebuf == NULL) {
  2391.       t = time (0);
  2392.       timebuf = localtime (&t);
  2393.     }
  2394.     buf = (char *) alloca (20);
  2395.     if (hp->type == T_DATE)
  2396.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  2397.           timebuf->tm_mday, timebuf->tm_year + 1900);
  2398.     else
  2399.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  2400.           timebuf->tm_sec);
  2401.     break;
  2402.  
  2403.   case T_SPEC_DEFINED:
  2404.     buf = " 0 ";        /* Assume symbol is not defined */
  2405.     ip = &instack[indepth];
  2406.     SKIP_WHITE_SPACE (ip->bufp);
  2407.     if (*ip->bufp == '(') {
  2408.       paren++;
  2409.       ip->bufp++;            /* Skip over the paren */
  2410.       SKIP_WHITE_SPACE (ip->bufp);
  2411.     }
  2412.  
  2413.     if (!is_idstart[*ip->bufp])
  2414.       goto oops;
  2415.     if (lookup (ip->bufp, -1, -1))
  2416.       buf = " 1 ";
  2417.     while (is_idchar[*ip->bufp])
  2418.       ++ip->bufp;
  2419.     SKIP_WHITE_SPACE (ip->bufp);
  2420.     if (paren) {
  2421.       if (*ip->bufp != ')')
  2422.     goto oops;
  2423.       ++ip->bufp;
  2424.     }
  2425.     break;
  2426.  
  2427. oops:
  2428.  
  2429.     error ("`defined' must be followed by ident or (ident)");
  2430.     break;
  2431.  
  2432.   default:
  2433.     error ("cccp error: invalid special hash type"); /* time for gdb */
  2434.     abort ();
  2435.   }
  2436.   len = strlen (buf);
  2437.   check_expand (op, len);
  2438.   bcopy (buf, op->bufp, len);
  2439.   op->bufp += len;
  2440.  
  2441.   return;
  2442. }
  2443.  
  2444.  
  2445. /* Routines to handle #directives */
  2446.  
  2447. /*
  2448.  * Process include file by reading it in and calling rescan.
  2449.  * Expects to see "fname" or <fname> on the input.
  2450.  */
  2451.  
  2452. do_include (buf, limit, op, keyword)
  2453.      U_CHAR *buf, *limit;
  2454.      FILE_BUF *op;
  2455.      struct directive *keyword;
  2456. {
  2457.   char *fname;        /* Dynamically allocated fname buffer */
  2458.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  2459.  
  2460.   struct file_name_list *stackp = include; /* Chain of dirs to search */
  2461.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  2462.   int flen;
  2463.  
  2464.   int f;            /* file number */
  2465.  
  2466.   int retried = 0;        /* Have already tried macro
  2467.                    expanding the include line*/
  2468.   FILE_BUF trybuf;        /* It got expanded into here */
  2469.   int system_header_p = 0;    /* 0 for "...", 1 for <...> */
  2470.  
  2471.   f= -1;            /* JF we iz paranoid! */
  2472.  
  2473. get_filename:
  2474.  
  2475.   fbeg = buf;
  2476.   SKIP_WHITE_SPACE (fbeg);
  2477.   /* Discard trailing whitespace so we can easily see
  2478.      if we have parsed all the significant chars we were given.  */
  2479.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  2480.  
  2481.   switch (*fbeg++) {
  2482.   case '\"':
  2483.     fend = fbeg;
  2484.     while (fend != limit && *fend != '\"')
  2485.       fend++;
  2486.     if (*fend == '\"' && fend + 1 == limit) {
  2487.       FILE_BUF *fp;
  2488.  
  2489.       /* We have "filename".  Figure out directory this source
  2490.      file is coming from and put it on the front of the list. */
  2491.  
  2492.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  2493.       if (ignore_srcdir) break;
  2494.  
  2495.       for (fp = &instack[indepth]; fp >= instack; fp--)
  2496.     {
  2497.       int n;
  2498.       char *ep,*nam;
  2499.       extern char *rindex ();
  2500.  
  2501.       if ((nam = fp->fname) != NULL) {
  2502.         /* Found a named file.  Figure out dir of the file,
  2503.            and put it in front of the search list.  */
  2504.         dsp[0].next = stackp;
  2505.         stackp = dsp;
  2506. #ifndef VMS
  2507.         ep = rindex (nam, '/');
  2508. #else                /* VMS */
  2509.         ep = rindex (nam, ']');
  2510.         if (ep == NULL) ep = rindex (nam, '>');
  2511.         if (ep == NULL) ep = rindex (nam, ':');
  2512.         if (ep != NULL) ep++;
  2513. #endif                /* VMS */
  2514.         if (ep != NULL) {
  2515.           n = ep - nam;
  2516.           dsp[0].fname = (char *) alloca (n + 1);
  2517.           strncpy (dsp[0].fname, nam, n);
  2518.           dsp[0].fname[n] = '\0';
  2519.           if (n > max_include_len) max_include_len = n;
  2520.         } else {
  2521.           dsp[0].fname = 0; /* Current directory */
  2522.         }
  2523.         break;
  2524.       }
  2525.     }
  2526.       break;
  2527.     }
  2528.     goto fail;
  2529.  
  2530.   case '<':
  2531.     fend = fbeg;
  2532.     while (fend != limit && *fend != '>') fend++;
  2533.     if (*fend == '>' && fend + 1 == limit) {
  2534.       system_header_p = 1;
  2535.       /* If -I-, start with the first -I dir after the -I-.  */
  2536.       if (first_bracket_include)
  2537.     stackp = first_bracket_include;
  2538.       break;
  2539.     }
  2540.     goto fail;
  2541.  
  2542.   default:
  2543.   fail:
  2544.     if (retried) {
  2545.       error ("#include expects \"fname\" or <fname>");
  2546.       return;
  2547.     } else {
  2548.       trybuf = expand_to_temp_buffer (buf, limit, 0);
  2549.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  2550.       bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
  2551.       limit = buf + (trybuf.bufp - trybuf.buf);
  2552.       free (trybuf.buf);
  2553.       retried++;
  2554.       goto get_filename;
  2555.     }
  2556.   }
  2557.  
  2558.   flen = fend - fbeg;
  2559.   fname = (char *) alloca (max_include_len + flen + 2);
  2560.   /* + 2 above for slash and terminating null.  */
  2561.  
  2562.   /* If specified file name is absolute, just open it.  */
  2563.  
  2564.   if (*fbeg == '/') {
  2565.     strncpy (fname, fbeg, flen);
  2566.     fname[flen] = 0;
  2567.     f = open (fname, O_RDONLY, 0666);
  2568.   } else {
  2569.     /* Search directory path, trying to open the file.
  2570.        Copy each filename tried into FNAME.  */
  2571.  
  2572.     for (; stackp; stackp = stackp->next) {
  2573.       if (stackp->fname) {
  2574.     strcpy (fname, stackp->fname);
  2575.     strcat (fname, "/");
  2576.     fname[strlen (fname) + flen] = 0;
  2577.       } else {
  2578.     fname[0] = 0;
  2579.       }
  2580.       strncat (fname, fbeg, flen);
  2581. #ifdef VMS
  2582.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  2583.          full VMS file specification */
  2584.       if (stackp->fname && (stackp->fname[0] != 0)) {
  2585.     /* Fix up the filename */
  2586.     hack_vms_include_specification (fname);
  2587.       } else {
  2588.           /* This is a normal VMS filespec, so use it unchanged.  */
  2589.     strncpy (fname, fbeg, flen);
  2590.     fname[flen] = 0;
  2591.       }
  2592. #endif /* VMS */
  2593.       if ((f = open (fname, O_RDONLY, 0666)) >= 0)
  2594.     break;
  2595.     }
  2596.   }
  2597.  
  2598.   if (f < 0) {
  2599.     strncpy (fname, fbeg, flen);
  2600.     fname[flen] = 0;
  2601.     error_from_errno (fname);
  2602.  
  2603.     /* For -M, add this file to the dependencies.  */
  2604.     if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2605.       if (system_header_p)
  2606.     warning ("nonexistent file <%.*s> omitted from dependency output",
  2607.          fend - fbeg, fbeg);
  2608.       else
  2609.     {
  2610.       deps_output (fbeg, fend - fbeg);
  2611.       deps_output (" ", 0);
  2612.     }
  2613.     }
  2614.   } else {
  2615.  
  2616.     /* Check to see if this include file is a once-only include file.
  2617.        If so, give up.  */
  2618.  
  2619.     struct file_name_list* ptr;
  2620.  
  2621.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  2622.       if (!strcmp (ptr->fname, fname)) {
  2623.     close (f);
  2624.         return;                /* This file was once'd. */
  2625.       }
  2626.     }
  2627.  
  2628.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  2629.       if (!strcmp (ptr->fname, fname))
  2630.         break;                /* This file was included before. */
  2631.     }
  2632.  
  2633.     if (ptr == 0) {
  2634.       /* This is the first time for this file.  */
  2635.       /* Add it to list of files included.  */
  2636.  
  2637.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2638.       ptr->next = all_include_files;
  2639.       all_include_files = ptr;
  2640.       ptr->fname = savestring (fname);
  2641.  
  2642.       /* For -M, add this file to the dependencies.  */
  2643.       if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2644.     deps_output (fname, strlen (fname));
  2645.     deps_output (" ", 0);
  2646.       }
  2647.     }   
  2648.  
  2649.     if (system_header_p)
  2650.       system_include_depth++;
  2651.  
  2652.     /* Actually process the file.  */
  2653.     finclude (f, fname, op);
  2654.  
  2655.     if (system_header_p)
  2656.       system_include_depth--;
  2657.  
  2658.     close (f);
  2659.   }
  2660. }
  2661.  
  2662. /* Process the contents of include file FNAME, already open on descriptor F,
  2663.    with output to OP.  */
  2664.  
  2665. finclude (f, fname, op)
  2666.      int f;
  2667.      char *fname;
  2668.      FILE_BUF *op;
  2669. {
  2670.   int st_mode;
  2671.   long st_size;
  2672.   long i;
  2673.   FILE_BUF *fp;            /* For input stack frame */
  2674.   int success = 0;
  2675.  
  2676.   CHECK_DEPTH (return;);
  2677.  
  2678.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  2679.     goto nope;        /* Impossible? */
  2680.  
  2681.   fp = &instack[indepth + 1];
  2682.   bzero (fp, sizeof (FILE_BUF));
  2683.   fp->fname = fname;
  2684.   fp->length = 0;
  2685.   fp->lineno = 1;
  2686.   fp->if_stack = if_stack;
  2687.  
  2688.   if (st_mode & S_IFREG) {
  2689.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2690.     fp->bufp = fp->buf;
  2691.  
  2692.     /* Read the file contents, knowing that st_size is an upper bound
  2693.        on the number of bytes we can read.  */
  2694.     while (st_size > 0) {
  2695.       i = read (f, fp->buf + fp->length, st_size);
  2696.       if (i <= 0) {
  2697.     if (i == 0) break;
  2698.     goto nope;
  2699.       }
  2700.       fp->length += i;
  2701.       st_size -= i;
  2702.     }
  2703.   }
  2704.   else {
  2705.     /* Cannot count its file size before reading.
  2706.        First read the entire file into heap and
  2707.        copy them into buffer on stack. */
  2708.  
  2709.     U_CHAR *bufp;
  2710.     U_CHAR *basep;
  2711.     int bsize = 2000;
  2712.  
  2713.     st_size = 0;
  2714.     basep = (U_CHAR *) xmalloc (bsize + 2);
  2715.     bufp = basep;
  2716.  
  2717.     for (;;) {
  2718.       i = read (f, bufp, bsize - st_size);
  2719.       if (i < 0)
  2720.     goto nope;      /* error! */
  2721.       if (i == 0)
  2722.     break;    /* End of file */
  2723.       st_size += i;
  2724.       bufp += i;
  2725.       if (bsize == st_size) {    /* Buffer is full! */
  2726.       bsize *= 2;
  2727.       basep = (U_CHAR *) xrealloc (basep, bsize + 2);
  2728.       bufp = basep + st_size;    /* May have moved */
  2729.     }
  2730.     }
  2731.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2732.     fp->bufp = fp->buf;
  2733.     bcopy (basep, fp->buf, st_size);
  2734.     fp->length = st_size;
  2735.     free (basep);
  2736.   }
  2737.  
  2738.   if (!no_trigraphs)
  2739.     trigraph_pcp (fp);
  2740.  
  2741.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  2742.     fp->buf[fp->length++] = '\n';
  2743.   fp->buf[fp->length] = '\0';
  2744.  
  2745.   success = 1;
  2746.   indepth++;
  2747.  
  2748.   output_line_command (fp, op, 0, enter_file);
  2749.   rescan (op, 0);
  2750.   indepth--;
  2751.   output_line_command (&instack[indepth], op, 0, leave_file);
  2752.  
  2753. nope:
  2754.  
  2755.   if (!success)
  2756.     perror_with_name (fname);
  2757.  
  2758.   close (f);
  2759. }
  2760.  
  2761. /* The arglist structure is built by do_define to tell
  2762.    collect_definition where the argument names begin.  That
  2763.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  2764.    would contain pointers to the strings x, y, and z.
  2765.    Collect_definition would then build a DEFINITION node,
  2766.    with reflist nodes pointing to the places x, y, and z had
  2767.    appeared.  So the arglist is just convenience data passed
  2768.    between these two routines.  It is not kept around after
  2769.    the current #define has been processed and entered into the
  2770.    hash table. */
  2771.  
  2772. struct arglist {
  2773.   struct arglist *next;
  2774.   U_CHAR *name;
  2775.   int length;
  2776.   int argno;
  2777. };
  2778.  
  2779. /* Process a #define command.
  2780. BUF points to the contents of the #define command, as a continguous string.
  2781. LIMIT points to the first character past the end of the definition.
  2782. KEYWORD is the keyword-table entry for #define.  */
  2783.  
  2784. do_define (buf, limit, op, keyword)
  2785.      U_CHAR *buf, *limit;
  2786.      FILE_BUF *op;
  2787.      struct directive *keyword;
  2788. {
  2789.   U_CHAR *bp;            /* temp ptr into input buffer */
  2790.   U_CHAR *symname;        /* remember where symbol name starts */
  2791.   int sym_length;        /* and how long it is */
  2792.  
  2793.   DEFINITION *defn;
  2794.   int arglengths = 0;        /* Accumulate lengths of arg names
  2795.                    plus number of args.  */
  2796.   int hashcode;
  2797.  
  2798.   bp = buf;
  2799.  
  2800.   while (is_hor_space[*bp])
  2801.     bp++;
  2802.  
  2803.   symname = bp;            /* remember where it starts */
  2804.   while (is_idchar[*bp] && bp < limit) {
  2805.     bp++;
  2806.   }
  2807.   sym_length = bp - symname;
  2808.   if (sym_length == 0)
  2809.     error ("invalid macro name");
  2810.   else if (!is_idstart[*symname]) {
  2811.     U_CHAR *msg;            /* what pain... */
  2812.     msg = (U_CHAR *) alloca (sym_length + 1);
  2813.     bcopy (symname, msg, sym_length);
  2814.     msg[sym_length] = 0;
  2815.     error ("invalid macro name `%s'", msg);
  2816.   }
  2817.  
  2818.   /* lossage will occur if identifiers or control keywords are broken
  2819.      across lines using backslash.  This is not the right place to take
  2820.      care of that. */
  2821.  
  2822.   if (*bp == '(') {
  2823.     struct arglist *arg_ptrs = NULL;
  2824.     int argno = 0;
  2825.  
  2826.     bp++;            /* skip '(' */
  2827.     SKIP_WHITE_SPACE (bp);
  2828.  
  2829.     /* Loop over macro argument names.  */
  2830.     while (*bp != ')') {
  2831.       struct arglist *temp;
  2832.  
  2833.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  2834.       temp->name = bp;
  2835.       temp->next = arg_ptrs;
  2836.       temp->argno = argno++;
  2837.       arg_ptrs = temp;
  2838.  
  2839.       if (!is_idstart[*bp])
  2840.     warning ("parameter name starts with a digit in #define");
  2841.  
  2842.       /* Find the end of the arg name.  */
  2843.       while (is_idchar[*bp]) {
  2844.     bp++;
  2845.       }
  2846.       temp->length = bp - temp->name;
  2847.       arglengths += temp->length + 2;
  2848.       SKIP_WHITE_SPACE (bp);
  2849.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  2850.     error ("badly punctuated parameter list in #define");
  2851.     goto nope;
  2852.       }
  2853.       if (*bp == ',') {
  2854.     bp++;
  2855.     SKIP_WHITE_SPACE (bp);
  2856.       }
  2857.       if (bp >= limit) {
  2858.     error ("unterminated parameter list in #define");
  2859.     goto nope;
  2860.       }
  2861.     }
  2862.  
  2863.     ++bp;            /* skip paren */
  2864.     /* Skip exactly one space or tab if any.  */
  2865.     if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
  2866.     /* now everything from bp before limit is the definition. */
  2867.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  2868.  
  2869.     /* Now set defn->argnames to the result of concatenating
  2870.        the argument names in reverse order
  2871.        with comma-space between them.  */
  2872.     defn->argnames = (U_CHAR *) xmalloc (arglengths + 1);
  2873.     {
  2874.       struct arglist *temp;
  2875.       int i = 0;
  2876.       for (temp = arg_ptrs; temp; temp = temp->next) {
  2877.     bcopy (temp->name, &defn->argnames[i], temp->length);
  2878.     i += temp->length;
  2879.     if (temp->next != 0) {
  2880.       defn->argnames[i++] = ',';
  2881.       defn->argnames[i++] = ' ';
  2882.     }
  2883.       }
  2884.       defn->argnames[i] = 0;
  2885.     }
  2886.   } else {
  2887.     /* simple expansion or empty definition; gobble it */
  2888.     if (is_hor_space[*bp])
  2889.       ++bp;        /* skip exactly one blank/tab char */
  2890.     /* now everything from bp before limit is the definition. */
  2891.     defn = collect_expansion (bp, limit, -1, 0);
  2892.     defn->argnames = (U_CHAR *) "";
  2893.   }
  2894.  
  2895.   hashcode = hashf (symname, sym_length, HASHSIZE);
  2896.  
  2897.   {
  2898.     HASHNODE *hp;
  2899.     if ((hp = lookup (symname, sym_length, hashcode)) != NULL) {
  2900.       if (hp->type != T_MACRO
  2901.       || compare_defs (defn, hp->value.defn)) {
  2902.     U_CHAR *msg;            /* what pain... */
  2903.     msg = (U_CHAR *) alloca (sym_length + 20);
  2904.     bcopy (symname, msg, sym_length);
  2905.     strcpy ((char *) (msg + sym_length), " redefined");
  2906.     warning (msg);
  2907.       }
  2908.       /* Replace the old definition.  */
  2909.       hp->type = T_MACRO;
  2910.       hp->value.defn = defn;
  2911.     } else
  2912.       install (symname, sym_length, T_MACRO, defn, hashcode);
  2913.   }
  2914.  
  2915.   return 0;
  2916.  
  2917. nope:
  2918.  
  2919.   return 1;
  2920. }
  2921.  
  2922. /*
  2923.  * return zero if two DEFINITIONs are isomorphic
  2924.  */
  2925. int
  2926. compare_defs (d1, d2)
  2927.      DEFINITION *d1, *d2;
  2928. {
  2929.   register struct reflist *a1, *a2;
  2930.   register U_CHAR *p1 = d1->expansion;
  2931.   register U_CHAR *p2 = d2->expansion;
  2932.   int first = 1;
  2933.  
  2934.   if (d1->nargs != d2->nargs)
  2935.     return 1;
  2936.   if (strcmp ((char *)d1->argnames, (char *)d2->argnames))
  2937.     return 1;
  2938.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  2939.        a1 = a1->next, a2 = a2->next) {
  2940.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  2941.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  2942.     || a1->argno != a2->argno
  2943.     || a1->stringify != a2->stringify
  2944.     || a1->raw_before != a2->raw_before
  2945.     || a1->raw_after != a2->raw_after)
  2946.       return 1;
  2947.     first = 0;
  2948.     p1 += a1->nchars;
  2949.     p2 += a2->nchars;
  2950.   }
  2951.   if (a1 != a2)
  2952.     return 1;
  2953.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  2954.              p2, d2->length - (p2 - d2->expansion), 1))
  2955.     return 1;
  2956.   return 0;
  2957. }
  2958.  
  2959. /* Return 1 if two parts of two macro definitions are effectively different.
  2960.    One of the parts starts at BEG1 and has LEN1 chars;
  2961.    the other has LEN2 chars at BEG2.
  2962.    Any sequence of whitespace matches any other sequence of whitespace.
  2963.    FIRST means these parts are the first of a macro definition;
  2964.     so ignore leading whitespace entirely.
  2965.    LAST means these parts are the last of a macro definition;
  2966.     so ignore trailing whitespace entirely.  */
  2967.  
  2968. comp_def_part (first, beg1, len1, beg2, len2, last)
  2969.      int first;
  2970.      U_CHAR *beg1, *beg2;
  2971.      int len1, len2;
  2972.      int last;
  2973. {
  2974.   register U_CHAR *end1 = beg1 + len1;
  2975.   register U_CHAR *end2 = beg2 + len2;
  2976.   if (first) {
  2977.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  2978.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  2979.   }
  2980.   if (last) {
  2981.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  2982.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  2983.   }
  2984.   while (beg1 != end1 && beg2 != end2) {
  2985.     if (is_space[*beg1] && is_space[*beg2]) {
  2986.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  2987.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  2988.     } else if (*beg1 == *beg2) {
  2989.       beg1++; beg2++;
  2990.     } else break;
  2991.   }
  2992.   return (beg1 != end1) || (beg2 != end2);
  2993. }
  2994.  
  2995. /* Read a replacement list for a macro with parameters.
  2996.    Build the DEFINITION structure.
  2997.    Reads characters of text starting at BUF until LIMIT.
  2998.    ARGLIST specifies the formal parameters to look for
  2999.    in the text of the definition; NARGS is the number of args
  3000.    in that list, or -1 for a macro name that wants no argument list.
  3001.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  3002.    and NAMELEN is its length in characters.
  3003.    
  3004. Note that comments and backslash-newlines have already been deleted
  3005. from the argument.  */
  3006.  
  3007. /* Leading and trailing Space, Tab, etc. are converted to markers
  3008.    Newline Space, Newline Tab, etc.
  3009.    Newline Space makes a space in the final output
  3010.    but is discarded if stringified.  (Newline Tab is similar but
  3011.    makes a Tab instead.)
  3012.  
  3013.    If there is no trailing whitespace, a Newline Space is added at the end
  3014.    to prevent concatenation that would be contrary to the standard.  */
  3015.  
  3016. DEFINITION *
  3017. collect_expansion (buf, end, nargs, arglist)
  3018.      U_CHAR *buf, *end;
  3019.      int nargs;
  3020.      struct arglist *arglist;
  3021. {
  3022.   DEFINITION *defn;
  3023.   register U_CHAR *p, *limit, *lastp, *exp_p;
  3024.   struct reflist *endpat = NULL;
  3025.   /* Pointer to first nonspace after last ## seen.  */
  3026.   U_CHAR *concat = 0;
  3027.   /* Pointer to first nonspace after last single-# seen.  */
  3028.   U_CHAR *stringify = 0;
  3029.   int maxsize;
  3030.   int expected_delimiter = '\0';
  3031.  
  3032.   /* Scan thru the replacement list, ignoring comments and quoted
  3033.      strings, picking up on the macro calls.  It does a linear search
  3034.      thru the arg list on every potential symbol.  Profiling might say
  3035.      that something smarter should happen. */
  3036.  
  3037.   if (end < buf)
  3038.     abort ();
  3039.  
  3040.   /* Find the beginning of the trailing whitespace.  */
  3041.   /* Find end of leading whitespace.  */
  3042.   limit = end;
  3043.   p = buf;
  3044.   while (p < limit && is_space[limit[-1]]) limit--;
  3045.   while (p < limit && is_space[*p]) p++;
  3046.  
  3047.   /* Allocate space for the text in the macro definition.
  3048.      Leading and trailing whitespace chars need 2 bytes each.
  3049.      Each other input char may or may not need 1 byte,
  3050.      so this is an upper bound.
  3051.      The extra 2 are for invented trailing newline-marker and final null.  */
  3052.   maxsize = (sizeof (DEFINITION)
  3053.          + 2 * (end - limit) + 2 * (p - buf)
  3054.          + (limit - p) + 3);
  3055.   defn = (DEFINITION *) xcalloc (1, maxsize);
  3056.  
  3057.   defn->nargs = nargs;
  3058.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  3059.   lastp = exp_p;
  3060.  
  3061.   p = buf;
  3062.  
  3063.   /* Convert leading whitespace to Newline-markers.  */
  3064.   while (p < limit && is_space[*p]) {
  3065.     *exp_p++ = '\n';
  3066.     *exp_p++ = *p++;
  3067.   }
  3068.  
  3069.   /* Process the main body of the definition.  */
  3070.   while (p < limit) {
  3071.     int skipped_arg = 0;
  3072.     register U_CHAR c = *p++;
  3073.  
  3074.     *exp_p++ = c;
  3075.  
  3076.     if (!traditional) {
  3077.       switch (c) {
  3078.       case '\'':
  3079.       case '\"':
  3080.     for (; p < limit && *p != c; p++) {
  3081.       *exp_p++ = *p;
  3082.       if (*p == '\\') {
  3083.         *exp_p++ = *++p;
  3084.       }
  3085.     }
  3086.     *exp_p++ = *p++;
  3087.     break;
  3088.  
  3089.     /* Special hack: if a \# is written in the #define
  3090.        include a # in the definition.  This is useless for C code
  3091.        but useful for preprocessing other things.  */
  3092.  
  3093.       case '\\':
  3094.     if (p < limit && *p == '#') {
  3095.       /* Pass through this # */
  3096.       exp_p--;
  3097.       *exp_p++ = *p++;
  3098.     } else if (p < limit) {
  3099.       /* Otherwise backslash goes through but makes next char ordinary.  */
  3100.       *exp_p++ = *p++;
  3101.     }
  3102.     break;
  3103.  
  3104.       case '#':
  3105.     if (p < limit && *p == '#') {
  3106.       /* ##: concatenate preceding and following tokens.  */
  3107.       /* Take out the first #, discard preceding whitespace.  */
  3108.       exp_p--;
  3109.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  3110.         --exp_p;
  3111.       /* Skip the second #.  */
  3112.       p++;
  3113.       /* Discard following whitespace.  */
  3114.       SKIP_WHITE_SPACE (p);
  3115.       concat = p;
  3116.     } else {
  3117.       /* Single #: stringify following argument ref.
  3118.          Don't leave the # in the expansion.  */
  3119.       exp_p--;
  3120.       SKIP_WHITE_SPACE (p);
  3121.       if (p == limit || ! is_idstart[*p] || nargs <= 0)
  3122.         error ("# operator should be followed by a macro argument name\n");
  3123.       else
  3124.         stringify = p;
  3125.     }
  3126.     break;
  3127.       }
  3128.     } else {
  3129.       /* In -traditional mode, recognize arguments inside strings and
  3130.      and character constants, and ignore special properties of #.
  3131.      Arguments inside strings are considered "stringified", but no
  3132.      extra quote marks are supplied.  */
  3133.       switch (c) {
  3134.       case '\'':
  3135.       case '\"':
  3136.     if (expected_delimiter != '\0') {
  3137.       if (c == expected_delimiter)
  3138.         expected_delimiter = '\0';
  3139.     } else
  3140.       expected_delimiter = c;
  3141.     break;
  3142.  
  3143.       case '\\':
  3144.     /* Backslash quotes delimiters and itself, but not macro args.  */
  3145.     if (expected_delimiter != 0 && p < limit
  3146.         && (*p == expected_delimiter || *p == '\\')) {
  3147.       *exp_p++ = *p++;
  3148.       continue;
  3149.     }
  3150.     break;
  3151.  
  3152.       case '/':
  3153.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  3154.       break;
  3155.     if (*p == '*') {
  3156.       /* If we find a comment that wasn't removed by handle_directive,
  3157.          this must be -traditional.  So replace the comment with
  3158.          nothing at all.  */
  3159.       exp_p--;
  3160.       p += 1;
  3161.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  3162.         p++;
  3163.       /* Mark this as a concatenation-point, as if it had been ##.  */
  3164.       concat = p;
  3165.     }
  3166.     break;
  3167.       }
  3168.     }
  3169.  
  3170.     if (is_idchar[c] && nargs > 0) {
  3171.       U_CHAR *id_beg = p - 1;
  3172.       int id_len;
  3173.  
  3174.       --exp_p;
  3175.       while (p != limit && is_idchar[*p]) p++;
  3176.       id_len = p - id_beg;
  3177.  
  3178.       if (is_idstart[c]) {
  3179.     register struct arglist *arg;
  3180.  
  3181.     for (arg = arglist; arg != NULL; arg = arg->next) {
  3182.       struct reflist *tpat;
  3183.  
  3184.       if (arg->name[0] == c
  3185.           && arg->length == id_len
  3186.           && strncmp (arg->name, id_beg, id_len) == 0) {
  3187.         /* make a pat node for this arg and append it to the end of
  3188.            the pat list */
  3189.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  3190.         tpat->next = NULL;
  3191.         tpat->raw_before = concat == id_beg;
  3192.         tpat->raw_after = 0;
  3193.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  3194.                    : stringify == id_beg);
  3195.  
  3196.         if (endpat == NULL)
  3197.           defn->pattern = tpat;
  3198.         else
  3199.           endpat->next = tpat;
  3200.         endpat = tpat;
  3201.  
  3202.         tpat->argno = arg->argno;
  3203.         tpat->nchars = exp_p - lastp;
  3204.         {
  3205.           register U_CHAR *p1 = p;
  3206.           SKIP_WHITE_SPACE (p1);
  3207.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  3208.         tpat->raw_after = 1;
  3209.         }
  3210.         lastp = exp_p;    /* place to start copying from next time */
  3211.         skipped_arg = 1;
  3212.         break;
  3213.       }
  3214.     }
  3215.       }
  3216.  
  3217.       /* If this was not a macro arg, copy it into the expansion.  */
  3218.       if (! skipped_arg) {
  3219.     register U_CHAR *lim1 = p;
  3220.     p = id_beg;
  3221.     while (p != lim1)
  3222.       *exp_p++ = *p++;
  3223.     if (stringify == id_beg)
  3224.       error ("# operator should be followed by a macro argument name\n");
  3225.       }
  3226.     }
  3227.   }
  3228.  
  3229.   if (limit < end) {
  3230.     /* Convert trailing whitespace to Newline-markers.  */
  3231.     while (limit < end && is_space[*limit]) {
  3232.       *exp_p++ = '\n';
  3233.       *exp_p++ = *limit++;
  3234.     }
  3235.   } else if (!traditional) {
  3236.     /* There is no trailing whitespace, so invent some.  */
  3237.     *exp_p++ = '\n';
  3238.     *exp_p++ = ' ';
  3239.   }
  3240.  
  3241.   *exp_p = '\0';
  3242.  
  3243.   defn->length = exp_p - defn->expansion;
  3244.  
  3245.   /* Crash now if we overrun the allocated size.  */
  3246.   if (defn->length + 1 > maxsize)
  3247.     abort ();
  3248.  
  3249. #if 0
  3250. /* This isn't worth the time it takes.  */
  3251.   /* give back excess storage */
  3252.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  3253. #endif
  3254.  
  3255.   return defn;
  3256. }
  3257.  
  3258. /*
  3259.  * interpret #line command.  Remembers previously seen fnames
  3260.  * in its very own hash table.
  3261.  */
  3262. #define FNAME_HASHSIZE 37
  3263.  
  3264. do_line (buf, limit, op, keyword)
  3265.      U_CHAR *buf, *limit;
  3266.      FILE_BUF *op;
  3267.      struct directive *keyword;
  3268. {
  3269.   register U_CHAR *bp;
  3270.   FILE_BUF *ip = &instack[indepth];
  3271.   FILE_BUF tem;
  3272.   int new_lineno;
  3273.   enum file_change_code file_change = same_file;
  3274.  
  3275.   /* Expand any macros.  */
  3276.   tem = expand_to_temp_buffer (buf, limit, 0);
  3277.  
  3278.   /* Point to macroexpanded line, which is null-terminated now.  */
  3279.   bp = tem.buf;
  3280.   SKIP_WHITE_SPACE (bp);
  3281.  
  3282.   if (!isdigit (*bp)) {
  3283.     error ("invalid format #line command");
  3284.     return;
  3285.   }
  3286.  
  3287.   /* The Newline at the end of this line remains to be processed.
  3288.      To put the next line at the specified line number,
  3289.      we must store a line number now that is one less.  */
  3290.   new_lineno = atoi (bp) - 1;
  3291.  
  3292.   /* skip over the line number.  */
  3293.   while (isdigit (*bp))
  3294.     bp++;
  3295.   if (*bp && !is_space[*bp]) {
  3296.     error ("invalid format #line command");
  3297.     return;
  3298.   }
  3299.     
  3300.   SKIP_WHITE_SPACE (bp);
  3301.  
  3302.   if (*bp == '\"') {
  3303.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  3304.     HASHNODE *hp, **hash_bucket;
  3305.     U_CHAR *fname;
  3306.     int fname_length;
  3307.  
  3308.     fname = ++bp;
  3309.  
  3310.     while (*bp && *bp != '\"')
  3311.       bp++;
  3312.     if (*bp != '\"') {
  3313.       error ("invalid format #line command");
  3314.       return;
  3315.     }
  3316.  
  3317.     fname_length = bp - fname;
  3318.  
  3319.     bp++;
  3320.     SKIP_WHITE_SPACE (bp);
  3321.     if (*bp) {
  3322.       if (*bp == '1')
  3323.     file_change = enter_file;
  3324.       else if (*bp == '2')
  3325.     file_change = leave_file;
  3326.       else {
  3327.     error ("invalid format #line command");
  3328.     return;
  3329.       }
  3330.  
  3331.       bp++;
  3332.       SKIP_WHITE_SPACE (bp);
  3333.       if (*bp) {
  3334.     error ("invalid format #line command");
  3335.     return;
  3336.       }
  3337.     }
  3338.  
  3339.     hash_bucket =
  3340.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  3341.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  3342.       if (hp->length == fname_length &&
  3343.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  3344.     ip->fname = hp->value.cpval;
  3345.     break;
  3346.       }
  3347.     if (hp == 0) {
  3348.       /* Didn't find it; cons up a new one.  */
  3349.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  3350.       hp->next = *hash_bucket;
  3351.       *hash_bucket = hp;
  3352.  
  3353.       hp->length = fname_length;
  3354.       ip->fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  3355.       bcopy (fname, hp->value.cpval, fname_length);
  3356.     }
  3357.   } else if (*bp) {
  3358.     error ("invalid format #line command");
  3359.     return;
  3360.   }
  3361.  
  3362.   ip->lineno = new_lineno;
  3363.   output_line_command (ip, op, 0, file_change);
  3364.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  3365. }
  3366.  
  3367. /*
  3368.  * remove all definitions of symbol from symbol table.
  3369.  * according to un*x /lib/cpp, it is not an error to undef
  3370.  * something that has no definitions, so it isn't one here either.
  3371.  */
  3372. do_undef (buf, limit, op, keyword)
  3373.      U_CHAR *buf, *limit;
  3374.      FILE_BUF *op;
  3375.      struct directive *keyword;
  3376. {
  3377.   HASHNODE *hp;
  3378.  
  3379.   SKIP_WHITE_SPACE (buf);
  3380.  
  3381.   while ((hp = lookup (buf, -1, -1)) != NULL) {
  3382.     if (hp->type != T_MACRO)
  3383.       error ("undefining `%s'", hp->name);
  3384.     delete_macro (hp);
  3385.   }
  3386. }
  3387.  
  3388. /*
  3389.  * Report a fatal error detected by the program we are processing.
  3390.  * Use the text of the line in the error message, then terminate.
  3391.  * (We use error() because it prints the filename & line#.)
  3392.  */
  3393. do_error (buf, limit, op, keyword)
  3394.      U_CHAR *buf, *limit;
  3395.      FILE_BUF *op;
  3396.      struct directive *keyword;
  3397. {
  3398.   int length = limit - buf;
  3399.   char *copy = (char *) xmalloc (length + 1);
  3400.   bcopy (buf, copy, length);
  3401.   copy[length] = 0;
  3402.   SKIP_WHITE_SPACE (copy);
  3403.   error ("#error %s", copy);
  3404.   exit (FATAL_EXIT_CODE);
  3405. }
  3406.  
  3407. /* Remember the name of the current file being read from so that we can
  3408.    avoid ever including it again.  */
  3409.  
  3410. do_once ()
  3411. {
  3412.   int i;
  3413.   FILE_BUF *ip = NULL;
  3414.  
  3415.   for (i = indepth; i >= 0; i--)
  3416.     if (instack[i].fname != NULL) {
  3417.       ip = &instack[i];
  3418.       break;
  3419.     }
  3420.  
  3421.   if (ip != NULL)
  3422.     {
  3423.       struct file_name_list *new;
  3424.  
  3425.       new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  3426.       new->next = dont_repeat_files;
  3427.       dont_repeat_files = new;
  3428.       new->fname = savestring (ip->fname);
  3429.     }
  3430. }
  3431.  
  3432. /* #pragma and its argument line have already been copied to the output file.
  3433.    Here just check for recognized pragmas.  */
  3434.  
  3435. do_pragma (buf, limit)
  3436.      U_CHAR *buf, *limit;
  3437. {
  3438.   while (*buf == ' ' || *buf == '\t')
  3439.     buf++;
  3440.   if (!strncmp (buf, "once", 4))
  3441.     do_once ();
  3442. }
  3443.  
  3444. #if 0
  3445. /* This was a fun hack, but #pragma seems to start to be useful.
  3446.    By failing to recognize it, we pass it through unchanged to cc1.  */
  3447.  
  3448. /*
  3449.  * the behavior of the #pragma directive is implementation defined.
  3450.  * this implementation defines it as follows.
  3451.  */
  3452. do_pragma ()
  3453. {
  3454.   close (0);
  3455.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  3456.     goto nope;
  3457.   close (1);
  3458.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  3459.     goto nope;
  3460.   execl ("/usr/games/hack", "#pragma", 0);
  3461.   execl ("/usr/games/rogue", "#pragma", 0);
  3462.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  3463.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  3464. nope:
  3465.   fatal ("You are in a maze of twisty compiler features, all different");
  3466. }
  3467. #endif
  3468.  
  3469. /* Just ignore #sccs, on systems where we define it at all.  */
  3470. do_sccs ()
  3471. {
  3472.   if (pedantic)
  3473.     error ("ANSI C does not allow #sccs");
  3474. }
  3475.  
  3476. /*
  3477.  * handle #if command by
  3478.  *   1) inserting special `defined' keyword into the hash table
  3479.  *    that gets turned into 0 or 1 by special_symbol (thus,
  3480.  *    if the luser has a symbol called `defined' already, it won't
  3481.  *      work inside the #if command)
  3482.  *   2) rescan the input into a temporary output buffer
  3483.  *   3) pass the output buffer to the yacc parser and collect a value
  3484.  *   4) clean up the mess left from steps 1 and 2.
  3485.  *   5) call conditional_skip to skip til the next #endif (etc.),
  3486.  *      or not, depending on the value from step 3.
  3487.  */
  3488.  
  3489. do_if (buf, limit, op, keyword)
  3490.      U_CHAR *buf, *limit;
  3491.      FILE_BUF *op;
  3492.      struct directive *keyword;
  3493. {
  3494.   int value;
  3495.   FILE_BUF *ip = &instack[indepth];
  3496.  
  3497.   value = eval_if_expression (buf, limit - buf);
  3498.   conditional_skip (ip, value == 0, T_IF);
  3499. }
  3500.  
  3501. /*
  3502.  * handle a #elif directive by not changing  if_stack  either.
  3503.  * see the comment above do_else.
  3504.  */
  3505.  
  3506. do_elif (buf, limit, op, keyword)
  3507.      U_CHAR *buf, *limit;
  3508.      FILE_BUF *op;
  3509.      struct directive *keyword;
  3510. {
  3511.   int value;
  3512.   FILE_BUF *ip = &instack[indepth];
  3513.  
  3514.   if (if_stack == instack[indepth].if_stack) {
  3515.     error ("#elif not within a conditional");
  3516.     return;
  3517.   } else {
  3518.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3519.       error ("#elif after #else");
  3520.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3521.       if (if_stack->fname != NULL && ip->fname != NULL &&
  3522.       strcmp (if_stack->fname, ip->fname) != 0)
  3523.     fprintf (stderr, ", file %s", if_stack->fname);
  3524.       fprintf (stderr, ")\n");
  3525.     }
  3526.     if_stack->type = T_ELIF;
  3527.   }
  3528.  
  3529.   if (if_stack->if_succeeded)
  3530.     skip_if_group (ip, 0);
  3531.   else {
  3532.     value = eval_if_expression (buf, limit - buf);
  3533.     if (value == 0)
  3534.       skip_if_group (ip, 0);
  3535.     else {
  3536.       ++if_stack->if_succeeded;    /* continue processing input */
  3537.       output_line_command (ip, op, 1, same_file);
  3538.     }
  3539.   }
  3540. }
  3541.  
  3542. /*
  3543.  * evaluate a #if expression in BUF, of length LENGTH,
  3544.  * then parse the result as a C expression and return the value as an int.
  3545.  */
  3546. int
  3547. eval_if_expression (buf, length)
  3548.      U_CHAR *buf;
  3549.      int length;
  3550. {
  3551.   FILE_BUF temp_obuf;
  3552.   HASHNODE *save_defined;
  3553.   int value;
  3554.  
  3555.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, -1);
  3556.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
  3557.   delete_macro (save_defined);    /* clean up special symbol */
  3558.  
  3559.   value = parse_c_expression (temp_obuf.buf);
  3560.  
  3561.   free (temp_obuf.buf);
  3562.  
  3563.   return value;
  3564. }
  3565.  
  3566. /*
  3567.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  3568.  * then do or don't skip to the #endif/#else/#elif depending
  3569.  * on what directive is actually being processed.
  3570.  */
  3571. do_xifdef (buf, limit, op, keyword)
  3572.      U_CHAR *buf, *limit;
  3573.      FILE_BUF *op;
  3574.      struct directive *keyword;
  3575. {
  3576.   int skip;
  3577.   FILE_BUF *ip = &instack[indepth];
  3578.   U_CHAR *end; 
  3579.  
  3580.   /* Discard leading and trailing whitespace.  */
  3581.   SKIP_WHITE_SPACE (buf);
  3582.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  3583.  
  3584.   /* Find the end of the identifier at the beginning.  */
  3585.   for (end = buf; is_idchar[*end]; end++);
  3586.  
  3587.   if (end == buf) {
  3588.     skip = (keyword->type == T_IFDEF);
  3589.     if (! traditional)
  3590.       warning (end == limit ? "#%s with no argument"
  3591.            : "#%s argument starts with punctuation",
  3592.            keyword->name);
  3593.   } else {
  3594.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  3595.       warning ("#%s argument starts with a digit", keyword->name);
  3596.     else if (end != limit && !traditional)
  3597.       warning ("garbage at end of #%s argument", keyword->name);
  3598.  
  3599.     skip = (lookup (buf, end-buf, -1) == NULL) ^ (keyword->type == T_IFNDEF);
  3600.   }
  3601.  
  3602.   conditional_skip (ip, skip, T_IF);
  3603. }
  3604.  
  3605. /*
  3606.  * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  3607.  */
  3608. void
  3609. conditional_skip (ip, skip, type)
  3610.      FILE_BUF *ip;
  3611.      int skip;
  3612.      enum node_type type;
  3613. {
  3614.   IF_STACK_FRAME *temp;
  3615.  
  3616.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3617.   temp->fname = ip->fname;
  3618.   temp->lineno = ip->lineno;
  3619.   temp->next = if_stack;
  3620.   if_stack = temp;
  3621.  
  3622.   if_stack->type = type;
  3623.  
  3624.   if (skip != 0) {
  3625.     skip_if_group (ip, 0);
  3626.     return;
  3627.   } else {
  3628.     ++if_stack->if_succeeded;
  3629.     output_line_command (ip, &outbuf, 1, same_file);
  3630.   }
  3631. }
  3632.  
  3633. /*
  3634.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  3635.  * leaves input ptr at the sharp sign found.
  3636.  * If ANY is nonzero, return at next directive of any sort.
  3637.  */
  3638. void
  3639. skip_if_group (ip, any)
  3640.      FILE_BUF *ip;
  3641.      int any;
  3642. {
  3643.   register U_CHAR *bp = ip->bufp, *cp;
  3644.   register U_CHAR *endb = ip->buf + ip->length;
  3645.   struct directive *kt;
  3646.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  3647.   U_CHAR *beg_of_line = bp;
  3648.  
  3649.   while (bp < endb) {
  3650.     switch (*bp++) {
  3651.     case '/':            /* possible comment */
  3652.       if (*bp == '\\' && bp[1] == '\n')
  3653.     newline_fix (bp);
  3654.       if (*bp == '*'
  3655.       || (cplusplus && *bp == '/')) {
  3656.     ip->bufp = ++bp;
  3657.     bp = skip_to_end_of_comment (ip, &ip->lineno);
  3658.       }
  3659.       break;
  3660.     case '\"':
  3661.     case '\'':
  3662.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
  3663.       break;
  3664.     case '\\':
  3665.       /* Char after backslash loses its special meaning.  */
  3666.       if (bp < endb) {
  3667.     if (*bp == '\n')
  3668.       ++ip->lineno;        /* But do update the line-count.  */
  3669.     bp++;
  3670.       }
  3671.       break;
  3672.     case '\n':
  3673.       ++ip->lineno;
  3674.       beg_of_line = bp;
  3675.       break;
  3676.     case '#':
  3677.       ip->bufp = bp - 1;
  3678.  
  3679.       /* # keyword: a # must be first nonblank char on the line */
  3680.       if (beg_of_line == 0)
  3681.     break;
  3682.       /* Scan from start of line, skipping whitespace, comments
  3683.      and backslash-newlines, and see if we reach this #.
  3684.      If not, this # is not special.  */
  3685.       bp = beg_of_line;
  3686.       while (1) {
  3687.     if (is_hor_space[*bp])
  3688.       bp++;
  3689.     else if (*bp == '\\' && bp[1] == '\n')
  3690.       bp += 2;
  3691.     else if (*bp == '/' && bp[1] == '*') {
  3692.       bp += 2;
  3693.       while (!(*bp == '*' && bp[1] == '/'))
  3694.         bp++;
  3695.       bp += 2;
  3696.     }
  3697.     else if (cplusplus && *bp == '/' && bp[1] == '/') {
  3698.       bp += 2;
  3699.       while (*bp++ != '\n') ;
  3700.         }
  3701.     else break;
  3702.       }
  3703.       if (bp != ip->bufp) {
  3704.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  3705.     break;
  3706.       }
  3707.  
  3708.       bp = ip->bufp + 1;        /* point at '#' */
  3709.  
  3710.       /* Skip whitespace and \-newline.  */
  3711.       while (1) {
  3712.     if (is_hor_space[*bp])
  3713.       bp++;
  3714.     else if (*bp == '\\' && bp[1] == '\n')
  3715.       bp += 2;
  3716.     else break;
  3717.       }
  3718.  
  3719.       cp = bp;
  3720.  
  3721.       /* Now find end of directive name.
  3722.      If we encounter a backslash-newline, exchange it with any following
  3723.      symbol-constituents so that we end up with a contiguous name.  */
  3724.  
  3725.       while (1) {
  3726.     if (is_idchar[*bp])
  3727.       bp++;
  3728.     else {
  3729.       if (*bp == '\\' && bp[1] == '\n')
  3730.         name_newline_fix (bp);
  3731.       if (is_idchar[*bp])
  3732.         bp++;
  3733.       else break;
  3734.     }
  3735.       }
  3736.  
  3737.       for (kt = directive_table; kt->length >= 0; kt++) {
  3738.     IF_STACK_FRAME *temp;
  3739.     if (strncmp (cp, kt->name, kt->length) == 0
  3740.         && !is_idchar[cp[kt->length]]) {
  3741.  
  3742.       /* If we are asked to return on next directive,
  3743.          do so now.  */
  3744.       if (any)
  3745.         return;
  3746.  
  3747.       switch (kt->type) {
  3748.       case T_IF:
  3749.       case T_IFDEF:
  3750.       case T_IFNDEF:
  3751.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3752.         temp->next = if_stack;
  3753.         if_stack = temp;
  3754.         temp->lineno = ip->lineno;
  3755.         temp->fname = ip->fname;
  3756.         temp->type = kt->type;
  3757.         break;
  3758.       case T_ELSE:
  3759.       case T_ENDIF:
  3760.         if (pedantic && if_stack != save_if_stack)
  3761.           validate_else (bp);
  3762.       case T_ELIF:
  3763.         if (if_stack == instack[indepth].if_stack) {
  3764.           error ("#%s not within a conditional", kt->name);
  3765.           break;
  3766.         }
  3767.         else if (if_stack == save_if_stack)
  3768.           return;        /* found what we came for */
  3769.  
  3770.         if (kt->type != T_ENDIF) {
  3771.           if (if_stack->type == T_ELSE)
  3772.         error ("#else or #elif after #else");
  3773.           if_stack->type = kt->type;
  3774.           break;
  3775.         }
  3776.  
  3777.         temp = if_stack;
  3778.         if_stack = if_stack->next;
  3779.         free (temp);
  3780.         break;
  3781.       }
  3782.       break;
  3783.     }
  3784.       }
  3785.     }
  3786.   }
  3787.   ip->bufp = bp;
  3788.   /* after this returns, rescan will exit because ip->bufp
  3789.      now points to the end of the buffer.
  3790.      rescan is responsible for the error message also.  */
  3791. }
  3792.  
  3793. /*
  3794.  * handle a #else directive.  Do this by just continuing processing
  3795.  * without changing  if_stack ;  this is so that the error message
  3796.  * for missing #endif's etc. will point to the original #if.  It
  3797.  * is possible that something different would be better.
  3798.  */
  3799. do_else (buf, limit, op, keyword)
  3800.      U_CHAR *buf, *limit;
  3801.      FILE_BUF *op;
  3802.      struct directive *keyword;
  3803. {
  3804.   FILE_BUF *ip = &instack[indepth];
  3805.  
  3806.   if (pedantic) {
  3807.     SKIP_WHITE_SPACE (buf);
  3808.     if (buf != limit)
  3809.       warning ("text following #else violates ANSI standard");
  3810.   }
  3811.  
  3812.   if (if_stack == instack[indepth].if_stack) {
  3813.     error ("#else not within a conditional");
  3814.     return;
  3815.   } else {
  3816.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3817.       error ("#else after #else");
  3818.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3819.       if (strcmp (if_stack->fname, ip->fname) != 0)
  3820.     fprintf (stderr, ", file %s", if_stack->fname);
  3821.       fprintf (stderr, ")\n");
  3822.     }
  3823.     if_stack->type = T_ELSE;
  3824.   }
  3825.  
  3826.   if (if_stack->if_succeeded)
  3827.     skip_if_group (ip, 0);
  3828.   else {
  3829.     ++if_stack->if_succeeded;    /* continue processing input */
  3830.     output_line_command (ip, op, 1, same_file);
  3831.   }
  3832. }
  3833.  
  3834. /*
  3835.  * unstack after #endif command
  3836.  */
  3837. do_endif (buf, limit, op, keyword)
  3838.      U_CHAR *buf, *limit;
  3839.      FILE_BUF *op;
  3840.      struct directive *keyword;
  3841. {
  3842.   if (pedantic) {
  3843.     SKIP_WHITE_SPACE (buf);
  3844.     if (buf != limit)
  3845.       warning ("text following #endif violates ANSI standard");
  3846.   }
  3847.  
  3848.   if (if_stack == instack[indepth].if_stack)
  3849.     error ("unbalanced #endif");
  3850.   else {
  3851.     IF_STACK_FRAME *temp = if_stack;
  3852.     if_stack = if_stack->next;
  3853.     free (temp);
  3854.     output_line_command (&instack[indepth], op, 1, same_file);
  3855.   }
  3856. }
  3857.  
  3858. /* When an #else or #endif is found while skipping failed conditional,
  3859.    if -pedantic was specified, this is called to warn about text after
  3860.    the command name.  P points to the first char after the command name.  */
  3861.  
  3862. validate_else (p)
  3863.      register U_CHAR *p;
  3864. {
  3865.   /* Advance P over whitespace and comments.  */
  3866.   while (1) {
  3867.     if (*p == '\\' && p[1] == '\n')
  3868.       p += 2;
  3869.     if (is_hor_space[*p])
  3870.       p++;
  3871.     else if (*p == '/') {
  3872.       if (p[1] == '\\' && p[2] == '\n')
  3873.     newline_fix (p + 1);
  3874.       if (p[1] == '*') {
  3875.     p += 2;
  3876.     /* Don't bother warning about unterminated comments
  3877.        since that will happen later.  Just be sure to exit.  */
  3878.     while (*p) {
  3879.       if (p[1] == '\\' && p[2] == '\n')
  3880.         newline_fix (p + 1);
  3881.       if (*p == '*' && p[1] == '/') {
  3882.         p += 2;
  3883.         break;
  3884.       }
  3885.       p++;
  3886.     }
  3887.       }
  3888.       else if (cplusplus && p[1] == '/') {
  3889.     p += 2;
  3890.     while (*p && *p++ != '\n') ;
  3891.       }
  3892.     } else break;
  3893.   }
  3894.   if (*p && *p != '\n')
  3895.     warning ("text following #else or #endif violates ANSI standard");
  3896. }
  3897.  
  3898. /*
  3899.  * Skip a comment, assuming the input ptr immediately follows the
  3900.  * initial slash-star.  Bump line counter as necessary.
  3901.  * (The canonical line counter is &ip->lineno).
  3902.  * Don't use this routine (or the next one) if bumping the line
  3903.  * counter is not sufficient to deal with newlines in the string.
  3904.  */
  3905. U_CHAR *
  3906. skip_to_end_of_comment (ip, line_counter)
  3907.      register FILE_BUF *ip;
  3908.      int *line_counter;        /* place to remember newlines, or NULL */
  3909. {
  3910.   register U_CHAR *limit = ip->buf + ip->length;
  3911.   register U_CHAR *bp = ip->bufp;
  3912.   FILE_BUF *op = &outbuf;    /* JF */
  3913.   int output = put_out_comments && !line_counter;
  3914.  
  3915.     /* JF this line_counter stuff is a crock to make sure the
  3916.        comment is only put out once, no matter how many times
  3917.        the comment is skipped.  It almost works */
  3918.   if (output) {
  3919.     *op->bufp++ = '/';
  3920.     *op->bufp++ = '*';
  3921.   }
  3922.   if (cplusplus && bp[-1] == '/') {
  3923.     if (output) {
  3924.       while (bp < limit)
  3925.     if ((*op->bufp++ = *bp++) == '\n') {
  3926.       bp--;
  3927.       break;
  3928.     }
  3929.       op->bufp[-1] = '*';
  3930.       *op->bufp++ = '/';
  3931.       *op->bufp++ = '\n';
  3932.     } else {
  3933.       while (bp < limit) {
  3934.     if (*bp++ == '\n') {
  3935.       bp--;
  3936.       break;
  3937.     }
  3938.       }
  3939.     }
  3940.     ip->bufp = bp;
  3941.     return bp;
  3942.   }
  3943.   while (bp < limit) {
  3944.     if (output)
  3945.       *op->bufp++ = *bp;
  3946.     switch (*bp++) {
  3947.     case '\n':
  3948.       if (line_counter != NULL)
  3949.     ++*line_counter;
  3950.       if (output)
  3951.     ++op->lineno;
  3952.       break;
  3953.     case '*':
  3954.       if (*bp == '\\' && bp[1] == '\n')
  3955.     newline_fix (bp);
  3956.       if (*bp == '/') {
  3957.         if (output)
  3958.       *op->bufp++ = '/';
  3959.     ip->bufp = ++bp;
  3960.     return bp;
  3961.       }
  3962.       break;
  3963.     }
  3964.   }
  3965.   ip->bufp = bp;
  3966.   return bp;
  3967. }
  3968.  
  3969. /*
  3970.  * Skip over a quoted string.  BP points to the opening quote.
  3971.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  3972.  * START_LINE is the line number of the starting point (but it need
  3973.  * not be valid if the starting point is inside a macro expansion).
  3974.  *
  3975.  * The input stack state is not changed.
  3976.  *
  3977.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  3978.  * for each newline passed.
  3979.  *
  3980.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  3981.  * if we pass a backslash-newline.
  3982.  *
  3983.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  3984.  */
  3985. U_CHAR *
  3986. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  3987.      register U_CHAR *bp;
  3988.      register U_CHAR *limit;
  3989.      int start_line;
  3990.      int *count_newlines;
  3991.      int *backslash_newlines_p;
  3992.      int *eofp;
  3993. {
  3994.   register U_CHAR c, match;
  3995.  
  3996.   match = *bp++;
  3997.   while (1) {
  3998.     if (bp >= limit) {
  3999.       error_with_line (line_for_error (start_line),
  4000.                "unterminated string or character constant");
  4001.       if (eofp)
  4002.     *eofp = 1;
  4003.       break;
  4004.     }
  4005.     c = *bp++;
  4006.     if (c == '\\') {
  4007.       while (*bp == '\\' && bp[1] == '\n') {
  4008.     if (backslash_newlines_p)
  4009.       *backslash_newlines_p = 1;
  4010.     if (count_newlines)
  4011.       ++*count_newlines;
  4012.     bp += 2;
  4013.       }
  4014.       if (*bp == '\n' && count_newlines) {
  4015.     if (backslash_newlines_p)
  4016.       *backslash_newlines_p = 1;
  4017.     ++*count_newlines;
  4018.       }
  4019.       bp++;
  4020.     } else if (c == '\n') {
  4021.       if (traditional) {
  4022.      /* Unterminated strings and character constants are 'legal'.  */
  4023.      bp--;    /* Don't consume the newline. */
  4024.      if (eofp)
  4025.        *eofp = 1;
  4026.      break;
  4027.       }
  4028.       if (match == '\'') {
  4029.     error_with_line (line_for_error (start_line),
  4030.              "unterminated character constant");
  4031.     bp--;
  4032.     if (eofp)
  4033.       *eofp = 1;
  4034.     break;
  4035.       }
  4036.       if (traditional) {    /* Unterminated strings are 'legal'.  */
  4037.     if (eofp)
  4038.       *eofp = 1;
  4039.     break;
  4040.       }
  4041.       /* If not traditional, then allow newlines inside strings.  */
  4042.       if (count_newlines)
  4043.     ++*count_newlines;
  4044.     } else if (c == match)
  4045.       break;
  4046.   }
  4047.   return bp;
  4048. }
  4049.  
  4050. /*
  4051.  * write out a #line command, for instance, after an #include file.
  4052.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  4053.  * appear to be a no-op, and we can output a few newlines instead
  4054.  * if we want to increase the line number by a small amount.
  4055.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  4056.  */
  4057.  
  4058. void
  4059. output_line_command (ip, op, conditional, file_change)
  4060.      FILE_BUF *ip, *op;
  4061.      int conditional;
  4062.      enum file_change_code file_change;
  4063. {
  4064.   int len;
  4065.   char line_cmd_buf[500];
  4066.  
  4067.   if (no_line_commands
  4068.       || ip->fname == NULL
  4069.       || no_output) {
  4070.     op->lineno = ip->lineno;
  4071.     return;
  4072.   }
  4073.  
  4074.   if (conditional) {
  4075.     if (ip->lineno == op->lineno)
  4076.       return;
  4077.  
  4078.     /* If the inherited line number is a little too small,
  4079.        output some newlines instead of a #line command.  */
  4080.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  4081.       check_expand (op, 10);
  4082.       while (ip->lineno > op->lineno) {
  4083.     *op->bufp++ = '\n';
  4084.     op->lineno++;
  4085.       }
  4086.       return;
  4087.     }
  4088.   }
  4089.  
  4090. #ifdef OUTPUT_LINE_COMMANDS
  4091.   sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->fname);
  4092. #else
  4093.   sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
  4094. #endif
  4095.   if (file_change != same_file)
  4096.     strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
  4097.   len = strlen (line_cmd_buf);
  4098.   line_cmd_buf[len++] = '\n';
  4099.   check_expand (op, len + 1);
  4100.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  4101.     *op->bufp++ = '\n';
  4102.   bcopy (line_cmd_buf, op->bufp, len);
  4103.   op->bufp += len;
  4104.   op->lineno = ip->lineno;
  4105. }
  4106.  
  4107. /* This structure represents one parsed argument in a macro call.
  4108.    `raw' points to the argument text as written (`raw_length' is its length).
  4109.    `expanded' points to the argument's macro-expansion
  4110.    (its length is `expand_length').
  4111.    `stringified_length' is the length the argument would have
  4112.    if stringified.
  4113.    `free1' and `free2', if nonzero, point to blocks to be freed
  4114.    when the macro argument data is no longer needed.  */
  4115.  
  4116. struct argdata {
  4117.   U_CHAR *raw, *expanded;
  4118.   int raw_length, expand_length;
  4119.   int stringified_length;
  4120.   U_CHAR *free1, *free2;
  4121.   char newlines;
  4122.   char comments;
  4123. };
  4124.  
  4125. /* Expand a macro call.
  4126.    HP points to the symbol that is the macro being called.
  4127.    Put the result of expansion onto the input stack
  4128.    so that subsequent input by our caller will use it.
  4129.  
  4130.    If macro wants arguments, caller has already verified that
  4131.    an argument list follows; arguments come from the input stack.  */
  4132.  
  4133. void
  4134. macroexpand (hp, op)
  4135.      HASHNODE *hp;
  4136.      FILE_BUF *op;
  4137. {
  4138.   int nargs;
  4139.   DEFINITION *defn = hp->value.defn;
  4140.   register U_CHAR *xbuf;
  4141.   int xbuf_len;
  4142.   int start_line = instack[indepth].lineno;
  4143.  
  4144.   CHECK_DEPTH (return;);
  4145.  
  4146.   /* it might not actually be a macro.  */
  4147.   if (hp->type != T_MACRO) {
  4148.     special_symbol (hp, op);
  4149.     return;
  4150.   }
  4151.  
  4152.   nargs = defn->nargs;
  4153.  
  4154.   if (nargs >= 0) {
  4155.     register int i;
  4156.     struct argdata *args;
  4157.     char *parse_error = 0;
  4158.  
  4159.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  4160.  
  4161.     for (i = 0; i < nargs; i++) {
  4162.       args[i].raw = args[i].expanded = (U_CHAR *) "";
  4163.       args[i].raw_length = args[i].expand_length
  4164.     = args[i].stringified_length = 0;
  4165.       args[i].free1 = args[i].free2 = 0;
  4166.     }
  4167.  
  4168.     /* Parse all the macro args that are supplied.  I counts them.
  4169.        The first NARGS args are stored in ARGS.
  4170.        The rest are discarded.  */
  4171.     i = 0;
  4172.     do {
  4173.       /* Discard the open-parenthesis or comma before the next arg.  */
  4174.       ++instack[indepth].bufp;
  4175.       parse_error
  4176.     = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
  4177.       if (parse_error)
  4178.     {
  4179.       error_with_line (line_for_error (start_line), parse_error);
  4180.       break;
  4181.     }
  4182.       i++;
  4183.     } while (*instack[indepth].bufp != ')');
  4184.  
  4185.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  4186.     if (i == 1) {
  4187.       register U_CHAR *bp = args[0].raw;
  4188.       register U_CHAR *lim = bp + args[0].raw_length;
  4189.       while (bp != lim && is_space[*bp]) bp++;
  4190.       if (bp == lim)
  4191.     i = 0;
  4192.     }
  4193.  
  4194.     if (nargs == 0 && i > 0)
  4195.       error ("arguments given to macro `%s'", hp->name);
  4196.     else if (i < nargs) {
  4197.       /* traditional C allows foo() if foo wants one argument.  */
  4198.       if (nargs == 1 && i == 0 && traditional)
  4199.     ;
  4200.       else if (i == 0)
  4201.     error ("no args to macro `%s'", hp->name);
  4202.       else if (i == 1)
  4203.     error ("only 1 arg to macro `%s'", hp->name);
  4204.       else
  4205.     error ("only %d args to macro `%s'", i, hp->name);
  4206.     } else if (i > nargs)
  4207.       error ("too many (%d) args to macro `%s'", i, hp->name);
  4208.  
  4209.     /* Swallow the closeparen.  */
  4210.     ++instack[indepth].bufp;
  4211.  
  4212.     /* If macro wants zero args, we parsed the arglist for checking only.
  4213.        Read directly from the macro definition.  */
  4214.     if (nargs == 0) {
  4215.       xbuf = defn->expansion;
  4216.       xbuf_len = defn->length;
  4217.     } else {
  4218.       register U_CHAR *exp = defn->expansion;
  4219.       register int offset;    /* offset in expansion,
  4220.                    copied a piece at a time */
  4221.       register int totlen;    /* total amount of exp buffer filled so far */
  4222.  
  4223.       register struct reflist *ap;
  4224.  
  4225.       /* Macro really takes args.  Compute the expansion of this call.  */
  4226.  
  4227.       /* Compute length in characters of the macro's expansion.  */
  4228.       xbuf_len = defn->length;
  4229.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4230.     if (ap->stringify)
  4231.       xbuf_len += args[ap->argno].stringified_length;
  4232.     else if (ap->raw_before || ap->raw_after)
  4233.       xbuf_len += args[ap->argno].raw_length;
  4234.     else
  4235.       xbuf_len += args[ap->argno].expand_length;
  4236.       }
  4237.  
  4238.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  4239.  
  4240.       /* Generate in XBUF the complete expansion
  4241.      with arguments substituted in.
  4242.      TOTLEN is the total size generated so far.
  4243.      OFFSET is the index in the definition
  4244.      of where we are copying from.  */
  4245.       offset = totlen = 0;
  4246.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4247.     register struct argdata *arg = &args[ap->argno];
  4248.  
  4249.     for (i = 0; i < ap->nchars; i++)
  4250.       xbuf[totlen++] = exp[offset++];
  4251.  
  4252.     if (ap->stringify != 0) {
  4253.       int arglen = arg->raw_length;
  4254.       int escaped = 0;
  4255.       int in_string = 0;
  4256.       int c;
  4257.       i = 0;
  4258.       while (i < arglen
  4259.          && (c = arg->raw[i], is_space[c]))
  4260.         i++;
  4261.       while (i < arglen
  4262.          && (c = arg->raw[arglen - 1], is_space[c]))
  4263.         arglen--;
  4264.       if (!traditional)
  4265.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  4266.       for (; i < arglen; i++) {
  4267.         c = arg->raw[i];
  4268.  
  4269.         /* Special markers Newline Space
  4270.            generate nothing for a stringified argument.  */
  4271.         if (c == '\n' && arg->raw[i+1] != '\n') {
  4272.           i++;
  4273.           continue;
  4274.         }
  4275.  
  4276.         /* Internal sequences of whitespace are replaced by one space.  */
  4277.         if (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c]) {
  4278.           while (1) {
  4279.         if (c == '\n' && arg->raw[i+1] == '\n')
  4280.           i += 2;
  4281.         else if (c != '\n' && is_space[c])
  4282.           i++;
  4283.         else break;
  4284.         c = arg->raw[i];
  4285.           }
  4286.           i--;
  4287.           c = ' ';
  4288.         }
  4289.  
  4290.         if (escaped)
  4291.           escaped = 0;
  4292.         else {
  4293.           if (c == '\\')
  4294.         escaped = 1;
  4295.           if (in_string && c == in_string)
  4296.         in_string = 0;
  4297.           else if (c == '\"' || c == '\'')
  4298.         in_string = c;
  4299.         }
  4300.  
  4301.         /* Escape these chars */
  4302.         if (c == '\"' || (in_string && c == '\\'))
  4303.           xbuf[totlen++] = '\\';
  4304.         if (isprint (c))
  4305.           xbuf[totlen++] = c;
  4306.         else {
  4307.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  4308.           totlen += 4;
  4309.         }
  4310.       }
  4311.       if (!traditional)
  4312.         xbuf[totlen++] = '\"'; /* insert ending quote */
  4313.     } else if (ap->raw_before || ap->raw_after) {
  4314.       U_CHAR *p1 = arg->raw;
  4315.       U_CHAR *l1 = p1 + arg->raw_length;
  4316.       if (ap->raw_before) {
  4317.         while (p1 != l1 && is_space[*p1]) p1++;
  4318.         while (p1 != l1 && is_idchar[*p1])
  4319.           xbuf[totlen++] = *p1++;
  4320.         /* Delete any no-reexpansion marker that follows
  4321.            an identifier at the beginning of the argument
  4322.            if the argument is concatenated with what precedes it.  */
  4323.         if (p1[0] == '\n' && p1[1] == '-')
  4324.           p1 += 2;
  4325.       }
  4326.       if (ap->raw_after) {
  4327.         /* Arg is concatenated after: delete trailing whitespace,
  4328.            whitespace markers, and no-reexpansion markers.  */
  4329.         while (p1 != l1) {
  4330.           if (is_space[l1[-1]]) l1--;
  4331.           else if (l1[-1] == '-') {
  4332.         U_CHAR *p2 = l1 - 1;
  4333.         /* If a `-' is preceded by an odd number of newlines then it
  4334.            and the last newline are a no-reexpansion marker.  */
  4335.         while (p2 != p1 && p2[-1] == '\n') p2--;
  4336.         if ((l1 - 1 - p2) & 1) {
  4337.           l1 -= 2;
  4338.         }
  4339.         else break;
  4340.           }
  4341.           else break;
  4342.         }
  4343.       }
  4344.       bcopy (p1, xbuf + totlen, l1 - p1);
  4345.       totlen += l1 - p1;
  4346.     } else {
  4347.       bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
  4348.       totlen += arg->expand_length;
  4349.     }
  4350.  
  4351.     if (totlen > xbuf_len)
  4352.       abort ();
  4353.       }
  4354.  
  4355.       /* if there is anything left of the definition
  4356.      after handling the arg list, copy that in too. */
  4357.  
  4358.       for (i = offset; i < defn->length; i++)
  4359.     xbuf[totlen++] = exp[i];
  4360.  
  4361.       xbuf[totlen] = 0;
  4362.       xbuf_len = totlen;
  4363.  
  4364.       for (i = 0; i < nargs; i++) {
  4365.     if (args[i].free1 != 0)
  4366.       free (args[i].free1);
  4367.     if (args[i].free2 != 0)
  4368.       free (args[i].free2);
  4369.       }
  4370.     }
  4371.   } else {
  4372.     xbuf = defn->expansion;
  4373.     xbuf_len = defn->length;
  4374.   }
  4375.  
  4376.   /* Now put the expansion on the input stack
  4377.      so our caller will commence reading from it.  */
  4378.   {
  4379.     register FILE_BUF *ip2;
  4380.  
  4381.     ip2 = &instack[++indepth];
  4382.  
  4383.     ip2->fname = 0;
  4384.     ip2->lineno = 0;
  4385.     ip2->buf = xbuf;
  4386.     ip2->length = xbuf_len;
  4387.     ip2->bufp = xbuf;
  4388.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  4389.     ip2->macro = hp;
  4390.     ip2->if_stack = if_stack;
  4391.  
  4392.     /* Recursive macro use sometimes works traditionally.
  4393.        #define foo(x,y) bar(x(y,0), y)
  4394.        foo(foo, baz)  */
  4395.  
  4396.     if (!traditional)
  4397.       hp->type = T_DISABLED;
  4398.   }
  4399. }
  4400.  
  4401. /*
  4402.  * Parse a macro argument and store the info on it into *ARGPTR.
  4403.  * Return nonzero to indicate a syntax error.
  4404.  */
  4405.  
  4406. char *
  4407. macarg (argptr)
  4408.      register struct argdata *argptr;
  4409. {
  4410.   FILE_BUF *ip = &instack[indepth];
  4411.   int paren = 0;
  4412.   int newlines = 0;
  4413.   int comments = 0;
  4414.  
  4415.   /* Try to parse as much of the argument as exists at this
  4416.      input stack level.  */
  4417.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  4418.             &paren, &newlines, &comments);
  4419.  
  4420.   /* If we find the end of the argument at this level,
  4421.      set up *ARGPTR to point at it in the input stack.  */
  4422.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  4423.       && bp != ip->buf + ip->length) {
  4424.     if (argptr != 0) {
  4425.       argptr->raw = ip->bufp;
  4426.       argptr->raw_length = bp - ip->bufp;
  4427.     }
  4428.     ip->bufp = bp;
  4429.   } else {
  4430.     /* This input stack level ends before the macro argument does.
  4431.        We must pop levels and keep parsing.
  4432.        Therefore, we must allocate a temporary buffer and copy
  4433.        the macro argument into it.  */
  4434.     int bufsize = bp - ip->bufp;
  4435.     int extra = newlines;
  4436.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  4437.     int final_start = 0;
  4438.  
  4439.     bcopy (ip->bufp, buffer, bufsize);
  4440.     ip->bufp = bp;
  4441.     ip->lineno += newlines;
  4442.  
  4443.     while (bp == ip->buf + ip->length) {
  4444.       if (instack[indepth].macro == 0) {
  4445.     free (buffer);
  4446.     return "unterminated macro call";
  4447.       }
  4448.       ip->macro->type = T_MACRO;
  4449.       free (ip->buf);
  4450.       ip = &instack[--indepth];
  4451.       newlines = 0;
  4452.       comments = 0;
  4453.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  4454.             &newlines, &comments);
  4455.       final_start = bufsize;
  4456.       bufsize += bp - ip->bufp;
  4457.       extra += newlines;
  4458.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  4459.       bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
  4460.       ip->bufp = bp;
  4461.       ip->lineno += newlines;
  4462.     }
  4463.  
  4464.     /* Now, if arg is actually wanted, record its raw form,
  4465.        discarding comments and duplicating newlines in whatever
  4466.        part of it did not come from a macro expansion.
  4467.        EXTRA space has been preallocated for duplicating the newlines.
  4468.        FINAL_START is the index of the start of that part.  */
  4469.     if (argptr != 0) {
  4470.       argptr->raw = buffer;
  4471.       argptr->raw_length = bufsize;
  4472.       argptr->free1 = buffer;
  4473.       argptr->newlines = newlines;
  4474.       argptr->comments = comments;
  4475.       if ((newlines || comments) && ip->fname != 0)
  4476.     argptr->raw_length
  4477.       = final_start +
  4478.         discard_comments (argptr->raw + final_start,
  4479.                   argptr->raw_length - final_start,
  4480.                   newlines);
  4481.       argptr->raw[argptr->raw_length] = 0;
  4482.       if (argptr->raw_length > bufsize + extra)
  4483.     abort ();
  4484.     }
  4485.   }
  4486.  
  4487.   /* If we are not discarding this argument,
  4488.      macroexpand it and compute its length as stringified.
  4489.      All this info goes into *ARGPTR.  */
  4490.  
  4491.   if (argptr != 0) {
  4492.     FILE_BUF obuf;
  4493.     register U_CHAR *buf, *lim;
  4494.     register int totlen;
  4495.  
  4496.     obuf = expand_to_temp_buffer (argptr->raw,
  4497.                   argptr->raw + argptr->raw_length,
  4498.                   1);
  4499.  
  4500.     argptr->expanded = obuf.buf;
  4501.     argptr->expand_length = obuf.length;
  4502.     argptr->free2 = obuf.buf;
  4503.  
  4504.     buf = argptr->raw;
  4505.     lim = buf + argptr->raw_length;
  4506.  
  4507.     while (buf != lim && is_space[*buf])
  4508.       buf++;
  4509.     while (buf != lim && is_space[lim[-1]])
  4510.       lim--;
  4511.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  4512.     while (buf != lim) {
  4513.       register U_CHAR c = *buf++;
  4514.       totlen++;
  4515.       /* Internal sequences of whitespace are replaced by one space.  */
  4516.       if (is_space[c])
  4517.     SKIP_ALL_WHITE_SPACE (buf);
  4518.       else if (c == '\"' || c == '\\') /* escape these chars */
  4519.     totlen++;
  4520.       else if (!isprint (c))
  4521.     totlen += 3;
  4522.     }
  4523.     argptr->stringified_length = totlen;
  4524.   }
  4525.   return 0;
  4526. }
  4527.  
  4528. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  4529.    counting parens in *DEPTHPTR,
  4530.    and return if reach LIMIT
  4531.    or before a `)' that would make *DEPTHPTR negative
  4532.    or before a comma when *DEPTHPTR is zero.
  4533.    Single and double quotes are matched and termination
  4534.    is inhibited within them.  Comments also inhibit it.
  4535.    Value returned is pointer to stopping place.
  4536.  
  4537.    Increment *NEWLINES each time a newline is passed.
  4538.    Set *COMMENTS to 1 if a comment is seen.  */
  4539.  
  4540. U_CHAR *
  4541. macarg1 (start, limit, depthptr, newlines, comments)
  4542.      U_CHAR *start;
  4543.      register U_CHAR *limit;
  4544.      int *depthptr, *newlines, *comments;
  4545. {
  4546.   register U_CHAR *bp = start;
  4547.  
  4548.   while (bp < limit) {
  4549.     switch (*bp) {
  4550.     case '(':
  4551.       (*depthptr)++;
  4552.       break;
  4553.     case ')':
  4554.       if (--(*depthptr) < 0)
  4555.     return bp;
  4556.       break;
  4557.     case '\\':
  4558.       /* Backslash makes following char not special.  */
  4559.       if (bp + 1 < limit)
  4560.     {
  4561.       bp++;
  4562.       /* But count source lines anyway.  */
  4563.       if (*bp == '\n')
  4564.         ++*newlines;
  4565.     }
  4566.       break;
  4567.     case '\n':
  4568.       ++*newlines;
  4569.       break;
  4570.     case '/':
  4571.       if (bp[1] == '\\' && bp[2] == '\n')
  4572.     newline_fix (bp + 1);
  4573.       if (cplusplus && bp[1] == '/') {
  4574.     *comments = 1;
  4575.     bp += 2;
  4576.     while (bp < limit && *bp++ != '\n') ;
  4577.     ++*newlines;
  4578.     break;
  4579.       }
  4580.       if (bp[1] != '*' || bp + 1 >= limit)
  4581.     break;
  4582.       *comments = 1;
  4583.       bp += 2;
  4584.       while (bp + 1 < limit) {
  4585.     if (bp[0] == '*'
  4586.         && bp[1] == '\\' && bp[2] == '\n')
  4587.       newline_fix (bp + 1);
  4588.     if (bp[0] == '*' && bp[1] == '/')
  4589.       break;
  4590.     if (*bp == '\n') ++*newlines;
  4591.     bp++;
  4592.       }
  4593.       break;
  4594.     case '\'':
  4595.     case '\"':
  4596.       {
  4597.     int quotec;
  4598.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  4599.       if (*bp == '\\') {
  4600.         bp++;
  4601.         if (*bp == '\n')
  4602.           ++*newlines;
  4603.         while (*bp == '\\' && bp[1] == '\n') {
  4604.           bp += 2;
  4605.         }
  4606.       } else if (*bp == '\n') {
  4607.         ++*newlines;
  4608.         if (quotec == '\'')
  4609.           break;
  4610.       }
  4611.     }
  4612.       }
  4613.       break;
  4614.     case ',':
  4615.       if ((*depthptr) == 0)
  4616.     return bp;
  4617.       break;
  4618.     }
  4619.     bp++;
  4620.   }
  4621.  
  4622.   return bp;
  4623. }
  4624.  
  4625. /* Discard comments and duplicate newlines
  4626.    in the string of length LENGTH at START,
  4627.    except inside of string constants.
  4628.    The string is copied into itself with its beginning staying fixed.  
  4629.  
  4630.    NEWLINES is the number of newlines that must be duplicated.
  4631.    We assume that that much extra space is available past the end
  4632.    of the string.  */
  4633.  
  4634. int
  4635. discard_comments (start, length, newlines)
  4636.      U_CHAR *start;
  4637.      int length;
  4638.      int newlines;
  4639. {
  4640.   register U_CHAR *ibp;
  4641.   register U_CHAR *obp;
  4642.   register U_CHAR *limit;
  4643.   register int c;
  4644.  
  4645.   /* If we have newlines to duplicate, copy everything
  4646.      that many characters up.  Then, in the second part,
  4647.      we will have room to insert the newlines
  4648.      while copying down.
  4649.      NEWLINES may actually be too large, because it counts
  4650.      newlines in string constants, and we don't duplicate those.
  4651.      But that does no harm.  */
  4652.   if (newlines > 0) {
  4653.     ibp = start + length;
  4654.     obp = ibp + newlines;
  4655.     limit = start;
  4656.     while (limit != ibp)
  4657.       *--obp = *--ibp;
  4658.   }
  4659.  
  4660.   ibp = start + newlines;
  4661.   limit = start + length + newlines;
  4662.   obp = start;
  4663.  
  4664.   while (ibp < limit) {
  4665.     *obp++ = c = *ibp++;
  4666.     switch (c) {
  4667.     case '\n':
  4668.       /* Duplicate the newline.  */
  4669.       *obp++ = '\n';
  4670.       break;
  4671.  
  4672.     case '/':
  4673.       if (*ibp == '\\' && ibp[1] == '\n')
  4674.     newline_fix (ibp);
  4675.       /* Delete any comment.  */
  4676.       if (cplusplus && ibp[0] == '/') {
  4677.     obp--;
  4678.     ibp++;
  4679.     while (ibp < limit && *ibp++ != '\n') ;
  4680.     break;
  4681.       }
  4682.       if (ibp[0] != '*' || ibp + 1 >= limit)
  4683.     break;
  4684.       obp--;
  4685.       ibp++;
  4686.       while (ibp + 1 < limit) {
  4687.     if (ibp[0] == '*'
  4688.         && ibp[1] == '\\' && ibp[2] == '\n')
  4689.       newline_fix (ibp + 1);
  4690.     if (ibp[0] == '*' && ibp[1] == '/')
  4691.       break;
  4692.     ibp++;
  4693.       }
  4694.       ibp += 2;
  4695.       break;
  4696.  
  4697.     case '\'':
  4698.     case '\"':
  4699.       /* Notice and skip strings, so that we don't
  4700.      think that comments start inside them,
  4701.      and so we don't duplicate newlines in them.  */
  4702.       {
  4703.     int quotec = c;
  4704.     while (ibp < limit) {
  4705.       *obp++ = c = *ibp++;
  4706.       if (c == quotec)
  4707.         break;
  4708.       if (c == '\n' && quotec == '\'')
  4709.         break;
  4710.       if (c == '\\' && ibp < limit) {
  4711.         while (*ibp == '\\' && ibp[1] == '\n')
  4712.           ibp += 2;
  4713.         *obp++ = *ibp++;
  4714.       }
  4715.     }
  4716.       }
  4717.       break;
  4718.     }
  4719.   }
  4720.  
  4721.   return obp - start;
  4722. }
  4723.  
  4724. /*
  4725.  * error - print error message and increment count of errors.
  4726.  */
  4727. error (msg, arg1, arg2, arg3)
  4728.      char *msg;
  4729. {
  4730.   int i;
  4731.   FILE_BUF *ip = NULL;
  4732.  
  4733.   for (i = indepth; i >= 0; i--)
  4734.     if (instack[i].fname != NULL) {
  4735.       ip = &instack[i];
  4736.       break;
  4737.     }
  4738.  
  4739.   if (ip != NULL)
  4740.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4741.   fprintf (stderr, msg, arg1, arg2, arg3);
  4742.   fprintf (stderr, "\n");
  4743.   errors++;
  4744.   return 0;
  4745. }
  4746.  
  4747. /* Error including a message from `errno'.  */
  4748.  
  4749. error_from_errno (name)
  4750.      char *name;
  4751. {
  4752.   int i;
  4753.   FILE_BUF *ip = NULL;
  4754.   extern int errno, sys_nerr;
  4755.   extern char *sys_errlist[];
  4756.  
  4757.   for (i = indepth; i >= 0; i--)
  4758.     if (instack[i].fname != NULL) {
  4759.       ip = &instack[i];
  4760.       break;
  4761.     }
  4762.  
  4763.   if (ip != NULL)
  4764.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4765.  
  4766.   if (errno < sys_nerr)
  4767.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  4768.   else
  4769.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  4770.  
  4771.   errors++;
  4772.   return 0;
  4773. }
  4774.  
  4775. /* Print error message but don't count it.  */
  4776.  
  4777. warning (msg, arg1, arg2, arg3)
  4778.      char *msg;
  4779. {
  4780.   int i;
  4781.   FILE_BUF *ip = NULL;
  4782.  
  4783.   for (i = indepth; i >= 0; i--)
  4784.     if (instack[i].fname != NULL) {
  4785.       ip = &instack[i];
  4786.       break;
  4787.     }
  4788.  
  4789.   if (ip != NULL)
  4790.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4791.   fprintf (stderr, "warning: ");
  4792.   fprintf (stderr, msg, arg1, arg2, arg3);
  4793.   fprintf (stderr, "\n");
  4794.   return 0;
  4795. }
  4796.  
  4797. error_with_line (line, msg, arg1, arg2, arg3)
  4798.      int line;
  4799.      char *msg;
  4800. {
  4801.   int i;
  4802.   FILE_BUF *ip = NULL;
  4803.  
  4804.   for (i = indepth; i >= 0; i--)
  4805.     if (instack[i].fname != NULL) {
  4806.       ip = &instack[i];
  4807.       break;
  4808.     }
  4809.  
  4810.   if (ip != NULL)
  4811.     fprintf (stderr, "%s:%d: ", ip->fname, line);
  4812.   fprintf (stderr, msg, arg1, arg2, arg3);
  4813.   fprintf (stderr, "\n");
  4814.   errors++;
  4815.   return 0;
  4816. }
  4817.  
  4818. /* Return the line at which an error occurred.
  4819.    The error is not necessarily associated with the current spot
  4820.    in the input stack, so LINE says where.  LINE will have been
  4821.    copied from ip->lineno for the current input level.
  4822.    If the current level is for a file, we return LINE.
  4823.    But if the current level is not for a file, LINE is meaningless.
  4824.    In that case, we return the lineno of the innermost file.  */
  4825. int
  4826. line_for_error (line)
  4827.      int line;
  4828. {
  4829.   int i;
  4830.   int line1 = line;
  4831.  
  4832.   for (i = indepth; i >= 0; ) {
  4833.     if (instack[i].fname != 0)
  4834.       return line1;
  4835.     i--;
  4836.     if (i < 0)
  4837.       return 0;
  4838.     line1 = instack[i].lineno;
  4839.   }
  4840. }
  4841.  
  4842. /*
  4843.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  4844.  *
  4845.  * As things stand, nothing is ever placed in the output buffer to be
  4846.  * removed again except when it's KNOWN to be part of an identifier,
  4847.  * so flushing and moving down everything left, instead of expanding,
  4848.  * should work ok.
  4849.  */
  4850.  
  4851. int
  4852. grow_outbuf (obuf, needed)
  4853.      register FILE_BUF *obuf;
  4854.      register int needed;
  4855. {
  4856.   register U_CHAR *p;
  4857.   int minsize;
  4858.  
  4859.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  4860.     return;
  4861.  
  4862.   /* Make it at least twice as big as it is now.  */
  4863.   obuf->length *= 2;
  4864.   /* Make it have at least 150% of the free space we will need.  */
  4865.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  4866.   if (minsize > obuf->length)
  4867.     obuf->length = minsize;
  4868.  
  4869.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  4870.     memory_full ();
  4871.  
  4872.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  4873.   obuf->buf = p;
  4874. }
  4875.  
  4876. /* Symbol table for macro names and special symbols */
  4877.  
  4878. /*
  4879.  * install a name in the main hash table, even if it is already there.
  4880.  *   name stops with first non alphanumeric, except leading '#'.
  4881.  * caller must check against redefinition if that is desired.
  4882.  * delete_macro () removes things installed by install () in fifo order.
  4883.  * this is important because of the `defined' special symbol used
  4884.  * in #if, and also if pushdef/popdef directives are ever implemented.
  4885.  *
  4886.  * If LEN is >= 0, it is the length of the name.
  4887.  * Otherwise, compute the length by scanning the entire name.
  4888.  *
  4889.  * If HASH is >= 0, it is the precomputed hash code.
  4890.  * Otherwise, compute the hash code.
  4891.  */
  4892. HASHNODE *
  4893. install (name, len, type, value, hash)
  4894.      U_CHAR *name;
  4895.      int len;
  4896.      enum node_type type;
  4897.      int value;
  4898.      int hash;
  4899.         /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
  4900. {
  4901.   register HASHNODE *hp;
  4902.   register int i, bucket;
  4903.   register U_CHAR *p, *q;
  4904.  
  4905.   if (len < 0) {
  4906.     p = name;
  4907.     while (is_idchar[*p])
  4908.       p++;
  4909.     len = p - name;
  4910.   }
  4911.  
  4912.   if (hash < 0)
  4913.     hash = hashf (name, len, HASHSIZE);
  4914.  
  4915.   i = sizeof (HASHNODE) + len + 1;
  4916.   hp = (HASHNODE *) xmalloc (i);
  4917.   bucket = hash;
  4918.   hp->bucket_hdr = &hashtab[bucket];
  4919.   hp->next = hashtab[bucket];
  4920.   hashtab[bucket] = hp;
  4921.   hp->prev = NULL;
  4922.   if (hp->next != NULL)
  4923.     hp->next->prev = hp;
  4924.   hp->type = type;
  4925.   hp->length = len;
  4926.   hp->value.ival = value;
  4927.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  4928.   p = hp->name;
  4929.   q = name;
  4930.   for (i = 0; i < len; i++)
  4931.     *p++ = *q++;
  4932.   hp->name[len] = 0;
  4933.   return hp;
  4934. }
  4935.  
  4936. /*
  4937.  * find the most recent hash node for name name (ending with first
  4938.  * non-identifier char) installed by install
  4939.  *
  4940.  * If LEN is >= 0, it is the length of the name.
  4941.  * Otherwise, compute the length by scanning the entire name.
  4942.  *
  4943.  * If HASH is >= 0, it is the precomputed hash code.
  4944.  * Otherwise, compute the hash code.
  4945.  */
  4946. HASHNODE *
  4947. lookup (name, len, hash)
  4948.      U_CHAR *name;
  4949.      int len;
  4950.      int hash;
  4951. {
  4952.   register U_CHAR *bp;
  4953.   register HASHNODE *bucket;
  4954.  
  4955.   if (len < 0) {
  4956.     for (bp = name; is_idchar[*bp]; bp++) ;
  4957.     len = bp - name;
  4958.   }
  4959.  
  4960.   if (hash < 0)
  4961.     hash = hashf (name, len, HASHSIZE);
  4962.  
  4963.   bucket = hashtab[hash];
  4964.   while (bucket) {
  4965.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  4966.       return bucket;
  4967.     bucket = bucket->next;
  4968.   }
  4969.   return NULL;
  4970. }
  4971.  
  4972. /*
  4973.  * Delete a hash node.  Some weirdness to free junk from macros.
  4974.  * More such weirdness will have to be added if you define more hash
  4975.  * types that need it.
  4976.  */
  4977.  
  4978. /* Note that the DEFINITION of a macro is removed from the hash table
  4979.    but its storage is not freed.  This would be a storage leak
  4980.    except that it is not reasonable to keep undefining and redefining
  4981.    large numbers of macros many times.
  4982.    In any case, this is necessary, because a macro can be #undef'd
  4983.    in the middle of reading the arguments to a call to it.
  4984.    If #undef freed the DEFINITION, that would crash.  */
  4985.  
  4986. delete_macro (hp)
  4987.      HASHNODE *hp;
  4988. {
  4989.  
  4990.   if (hp->prev != NULL)
  4991.     hp->prev->next = hp->next;
  4992.   if (hp->next != NULL)
  4993.     hp->next->prev = hp->prev;
  4994.  
  4995.   /* make sure that the bucket chain header that
  4996.      the deleted guy was on points to the right thing afterwards. */
  4997.   if (hp == *hp->bucket_hdr)
  4998.     *hp->bucket_hdr = hp->next;
  4999.  
  5000. #if 0
  5001.   if (hp->type == T_MACRO) {
  5002.     DEFINITION *d = hp->value.defn;
  5003.     struct reflist *ap, *nextap;
  5004.  
  5005.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  5006.       nextap = ap->next;
  5007.       free (ap);
  5008.     }
  5009.     free (d);
  5010.   }
  5011. #endif
  5012.   free (hp);
  5013. }
  5014.  
  5015. /*
  5016.  * return hash function on name.  must be compatible with the one
  5017.  * computed a step at a time, elsewhere
  5018.  */
  5019. int
  5020. hashf (name, len, hashsize)
  5021.      register U_CHAR *name;
  5022.      register int len;
  5023.      int hashsize;
  5024. {
  5025.   register int r = 0;
  5026.  
  5027.   while (len--)
  5028.     r = HASHSTEP (r, *name++);
  5029.  
  5030.   return MAKE_POS (r) % hashsize;
  5031. }
  5032.  
  5033. /* Dump all macro definitions as #defines to stdout.  */
  5034.  
  5035. void
  5036. dump_all_macros ()
  5037. {
  5038.   int bucket;
  5039.  
  5040.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  5041.     register HASHNODE *hp;
  5042.  
  5043.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  5044.       if (hp->type == T_MACRO) {
  5045.     register DEFINITION *defn = hp->value.defn;
  5046.     struct reflist *ap;
  5047.     int offset;
  5048.     int concat;
  5049.  
  5050.  
  5051.     /* Print the definition of the macro HP.  */
  5052.  
  5053.     printf ("#define %s", hp->name);
  5054.     if (defn->nargs >= 0) {
  5055.       int i;
  5056.  
  5057.       printf ("(");
  5058.       for (i = 0; i < defn->nargs; i++) {
  5059.         dump_arg_n (defn, i);
  5060.         if (i + 1 < defn->nargs)
  5061.           printf (", ");
  5062.       }
  5063.       printf (")");
  5064.     }
  5065.  
  5066.     printf (" ");
  5067.  
  5068.     offset = 0;
  5069.     concat = 0;
  5070.     for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  5071.       dump_defn_1 (defn->expansion, offset, ap->nchars);
  5072.       if (ap->nchars != 0)
  5073.         concat = 0;
  5074.       offset += ap->nchars;
  5075.       if (ap->stringify)
  5076.         printf (" #");
  5077.       if (ap->raw_before && !concat)
  5078.         printf (" ## ");
  5079.       concat = 0;
  5080.       dump_arg_n (defn, ap->argno);
  5081.       if (ap->raw_after) {
  5082.         printf (" ## ");
  5083.         concat = 1;
  5084.       }
  5085.     }
  5086.     dump_defn_1 (defn->expansion, offset, defn->length - offset);
  5087.     printf ("\n");
  5088.       }
  5089.     }
  5090.   }
  5091. }
  5092.  
  5093. /* Output to stdout a substring of a macro definition.
  5094.    BASE is the beginning of the definition.
  5095.    Output characters START thru LENGTH.
  5096.    Discard newlines outside of strings, thus
  5097.    converting funny-space markers to ordinary spaces.  */
  5098.  
  5099. dump_defn_1 (base, start, length)
  5100.      U_CHAR *base;
  5101.      int start;
  5102.      int length;
  5103. {
  5104.   U_CHAR *p = base + start;
  5105.   U_CHAR *limit = base + start + length;
  5106.  
  5107.   while (p < limit) {
  5108.     if (*p != '\n')
  5109.       putchar (*p);
  5110.     else if (*p == '\"' || *p =='\'') {
  5111.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
  5112.       fwrite (p, p1 - p, 1, stdout);
  5113.       p = p1 - 1;
  5114.     }
  5115.     p++;
  5116.   }
  5117. }
  5118.  
  5119. /* Print the name of argument number ARGNUM of macro definition DEFN.
  5120.    Recall that DEFN->argnames contains all the arg names
  5121.    concatenated in reverse order with comma-space in between.  */
  5122.  
  5123. dump_arg_n (defn, argnum)
  5124.      DEFINITION *defn;
  5125.      int argnum;
  5126. {
  5127.   register U_CHAR *p = defn->argnames;
  5128.   while (argnum + 1 < defn->nargs) {
  5129.     p = (U_CHAR *) index (p, ' ') + 1;
  5130.     argnum++;
  5131.   }
  5132.  
  5133.   while (*p && *p != ',') {
  5134.     putchar (*p);
  5135.     p++;
  5136.   }
  5137. }
  5138.  
  5139. /* Initialize syntactic classifications of characters.  */
  5140.  
  5141. initialize_char_syntax ()
  5142. {
  5143.   register int i;
  5144.  
  5145.   /*
  5146.    * Set up is_idchar and is_idstart tables.  These should be
  5147.    * faster than saying (is_alpha (c) || c == '_'), etc.
  5148.    * Must do set up these things before calling any routines tthat
  5149.    * refer to them.
  5150.    */
  5151.   for (i = 'a'; i <= 'z'; i++) {
  5152.     is_idchar[i - 'a' + 'A'] = 1;
  5153.     is_idchar[i] = 1;
  5154.     is_idstart[i - 'a' + 'A'] = 1;
  5155.     is_idstart[i] = 1;
  5156.   }
  5157.   for (i = '0'; i <= '9'; i++)
  5158.     is_idchar[i] = 1;
  5159.   is_idchar['_'] = 1;
  5160.   is_idstart['_'] = 1;
  5161.   is_idchar['$'] = dollars_in_ident;
  5162.   is_idstart['$'] = dollars_in_ident;
  5163.  
  5164.   /* horizontal space table */
  5165.   is_hor_space[' '] = 1;
  5166.   is_hor_space['\t'] = 1;
  5167.   is_hor_space['\v'] = 1;
  5168.   is_hor_space['\f'] = 1;
  5169.  
  5170.   is_space[' '] = 1;
  5171.   is_space['\t'] = 1;
  5172.   is_space['\v'] = 1;
  5173.   is_space['\f'] = 1;
  5174.   is_space['\n'] = 1;
  5175. }
  5176.  
  5177. /* Initialize the built-in macros.  */
  5178.  
  5179. initialize_builtins ()
  5180. {
  5181.   install ("__LINE__", -1, T_SPECLINE, 0, -1);
  5182.   install ("__DATE__", -1, T_DATE, 0, -1);
  5183.   install ("__FILE__", -1, T_FILE, 0, -1);
  5184.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, -1);
  5185.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, -1);
  5186.   install ("__VERSION__", -1, T_VERSION, 0, -1);
  5187.   install ("__TIME__", -1, T_TIME, 0, -1);
  5188.   if (!traditional)
  5189.     install ("__STDC__", -1, T_CONST, 1, -1);
  5190. /*  install ("__GNU__", -1, T_CONST, 1, -1);  */
  5191. /*  This is supplied using a -D by the compiler driver
  5192.     so that it is present only when truly compiling with GNU C.  */
  5193. }
  5194.  
  5195. /*
  5196.  * process a given definition string, for initialization
  5197.  * If STR is just an identifier, define it with value 1.
  5198.  * If STR has anything after the identifier, then it should
  5199.  * be identifier-space-definition.
  5200.  */
  5201. make_definition (str)
  5202.      U_CHAR *str;
  5203. {
  5204.   FILE_BUF *ip;
  5205.   struct directive *kt;
  5206.   U_CHAR *buf, *p;
  5207.  
  5208.   buf = str;
  5209.   p = str;
  5210.   while (is_idchar[*p]) p++;
  5211.   if (p == str) {
  5212.     error ("malformed option `-D %s'", str);
  5213.     return;
  5214.   }
  5215.   if (*p == 0) {
  5216.     buf = (U_CHAR *) alloca (p - buf + 4);
  5217.     strcpy ((char *)buf, str);
  5218.     strcat ((char *)buf, " 1");
  5219.   } else if (*p != ' ') {
  5220.     error ("malformed option `-D %s'", str);
  5221.     return;
  5222.   }
  5223.   
  5224.   ip = &instack[++indepth];
  5225.   ip->fname = "*Initialization*";
  5226.  
  5227.   ip->buf = ip->bufp = buf;
  5228.   ip->length = strlen (buf);
  5229.   ip->lineno = 1;
  5230.   ip->macro = 0;
  5231.   ip->free_ptr = 0;
  5232.   ip->if_stack = if_stack;
  5233.  
  5234.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  5235.     ;
  5236.  
  5237.   /* pass NULL as output ptr to do_define since we KNOW it never
  5238.      does any output.... */
  5239.   do_define (buf, buf + strlen (buf) , NULL, kt);
  5240.   --indepth;
  5241. }
  5242.  
  5243. /* JF, this does the work for the -U option */
  5244. make_undef (str)
  5245.      U_CHAR *str;
  5246. {
  5247.   FILE_BUF *ip;
  5248.   struct directive *kt;
  5249.  
  5250.   ip = &instack[++indepth];
  5251.   ip->fname = "*undef*";
  5252.  
  5253.   ip->buf = ip->bufp = str;
  5254.   ip->length = strlen (str);
  5255.   ip->lineno = 1;
  5256.   ip->macro = 0;
  5257.   ip->free_ptr = 0;
  5258.   ip->if_stack = if_stack;
  5259.  
  5260.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  5261.     ;
  5262.  
  5263.   do_undef (str,str + strlen (str) - 1, NULL, kt);
  5264.   --indepth;
  5265. }
  5266.  
  5267. /* Add output to `deps_buffer' for the -M switch.
  5268.    STRING points to the text to be output.
  5269.    SIZE is the number of bytes, or 0 meaning output until a null.
  5270.    If SIZE is nonzero, we break the line first, if it is long enough.  */
  5271.  
  5272. deps_output (string, size)
  5273.      char *string;
  5274.      int size;
  5275. {
  5276. #ifndef MAX_OUTPUT_COLUMNS
  5277. #define MAX_OUTPUT_COLUMNS 75
  5278. #endif
  5279.   if (size != 0 && deps_column != 0
  5280.       && size + deps_column > MAX_OUTPUT_COLUMNS) {
  5281.     deps_output ("\\\n  ", 0);
  5282.     deps_column = 0;
  5283.   }
  5284.  
  5285.   if (size == 0)
  5286.     size = strlen (string);
  5287.  
  5288.   if (deps_size + size + 1 > deps_allocated_size) {
  5289.     deps_allocated_size = deps_size + size + 50;
  5290.     deps_allocated_size *= 2;
  5291.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  5292.   }
  5293.   bcopy (string, &deps_buffer[deps_size], size);
  5294.   deps_size += size;
  5295.   deps_column += size;
  5296.   deps_buffer[deps_size] = 0;
  5297. }
  5298.  
  5299. #ifndef BSD
  5300. #ifndef BSTRING
  5301.  
  5302. void
  5303. bzero (b, length)
  5304.      register char *b;
  5305.      register int length;
  5306. {
  5307. #ifdef VMS
  5308.   short zero = 0;
  5309.   long max_str = 65535;
  5310.  
  5311.   while (length > max_str) {
  5312.     (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  5313.     length -= max_str;
  5314.     b += max_str;
  5315.   }
  5316.   (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  5317. #else
  5318.   while (length-- > 0)
  5319.     *b++ = 0;
  5320. #endif /* not VMS */
  5321. }
  5322.  
  5323. void
  5324. bcopy (b1, b2, length)
  5325.      register char *b1;
  5326.      register char *b2;
  5327.      register int length;
  5328. {
  5329. #ifdef VMS
  5330.   long max_str = 65535;
  5331.  
  5332.   while (length > max_str) {
  5333.     (void) LIB$MOVC3 (&max_str, b1, b2);
  5334.     length -= max_str;
  5335.     b1 += max_str;
  5336.     b2 += max_str;
  5337.   }
  5338.   (void) LIB$MOVC3 (&length, b1, b2);
  5339. #else
  5340.   while (length-- > 0)
  5341.     *b2++ = *b1++;
  5342. #endif /* not VMS */
  5343. }
  5344.  
  5345. int
  5346. bcmp (b1, b2, length)    /* This could be a macro! */
  5347.      register char *b1;
  5348.      register char *b2;
  5349.       register int length;
  5350. {
  5351. #ifdef VMS
  5352.    struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  5353.    struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  5354.  
  5355.    return STR$COMPARE (&src1, &src2);
  5356. #else
  5357.    while (length-- > 0)
  5358.      if (*b1++ != *b2++)
  5359.        return 1;
  5360.  
  5361.    return 0;
  5362. #endif /* not VMS */
  5363. }
  5364. #endif /* not BSTRING */
  5365. #endif /* not BSD */
  5366.  
  5367.  
  5368. void
  5369. fatal (str, arg)
  5370.      char *str, *arg;
  5371. {
  5372.   fprintf (stderr, "%s: ", progname);
  5373.   fprintf (stderr, str, arg);
  5374.   fprintf (stderr, "\n");
  5375.   exit (FATAL_EXIT_CODE);
  5376. }
  5377.  
  5378. /* More 'friendly' abort that prints the line and file.
  5379.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  5380.  
  5381. void
  5382. fancy_abort ()
  5383. {
  5384.   fatal ("Internal gcc abort.");
  5385. }
  5386.  
  5387. void
  5388. perror_with_name (name)
  5389.      char *name;
  5390. {
  5391.   extern int errno, sys_nerr;
  5392.   extern char *sys_errlist[];
  5393.  
  5394.   fprintf (stderr, "%s: ", progname);
  5395.   if (errno < sys_nerr)
  5396.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  5397.   else
  5398.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  5399.   errors++;
  5400. }
  5401.  
  5402. void
  5403. pfatal_with_name (name)
  5404.      char *name;
  5405. {
  5406.   perror_with_name (name);
  5407. #ifdef VMS
  5408.   exit (vaxc$errno);
  5409. #else
  5410.   exit (FATAL_EXIT_CODE);
  5411. #endif
  5412. }
  5413.  
  5414.  
  5415. void
  5416. memory_full ()
  5417. {
  5418.   fatal ("Memory exhausted.");
  5419. }
  5420.  
  5421.  
  5422. char *
  5423. xmalloc (size)
  5424.      int size;
  5425. {
  5426.   extern char *malloc ();
  5427.   register char *ptr = malloc (size);
  5428.   if (ptr != 0) return (ptr);
  5429.   memory_full ();
  5430.   /*NOTREACHED*/
  5431. }
  5432.  
  5433. char *
  5434. xrealloc (old, size)
  5435.      char *old;
  5436.      int size;
  5437. {
  5438.   extern char *realloc ();
  5439.   register char *ptr = realloc (old, size);
  5440.   if (ptr != 0) return (ptr);
  5441.   memory_full ();
  5442.   /*NOTREACHED*/
  5443. }
  5444.  
  5445. char *
  5446. xcalloc (number, size)
  5447.      int number, size;
  5448. {
  5449.   extern char *malloc ();
  5450.   register int total = number * size;
  5451.   register char *ptr = malloc (total);
  5452.   if (ptr != 0) {
  5453.     if (total > 100)
  5454.       bzero (ptr, total);
  5455.     else {
  5456.       /* It's not too long, so loop, zeroing by longs.
  5457.      It must be safe because malloc values are always well aligned.  */
  5458.       register long *zp = (long *) ptr;
  5459.       register long *zl = (long *) (ptr + total - 4);
  5460.       register int i = total - 4;
  5461.       while (zp < zl)
  5462.     *zp++ = 0;
  5463.       if (i < 0)
  5464.     i = 0;
  5465.       while (i < total)
  5466.     ptr[i++] = 0;
  5467.     }
  5468.     return ptr;
  5469.   }
  5470.   memory_full ();
  5471.   /*NOTREACHED*/
  5472. }
  5473.  
  5474. char *
  5475. savestring (input)
  5476.      char *input;
  5477. {
  5478.   int size = strlen (input);
  5479.   char *output = xmalloc (size + 1);
  5480.   strcpy (output, input);
  5481.   return output;
  5482. }
  5483.  
  5484. /* Get the file-mode and data size of the file open on FD
  5485.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  5486.  
  5487. int
  5488. file_size_and_mode (fd, mode_pointer, size_pointer)
  5489.      int fd;
  5490.      int *mode_pointer;
  5491.      long int *size_pointer;
  5492. {
  5493.   struct stat sbuf;
  5494.  
  5495.   if (fstat (fd, &sbuf) < 0) return (-1);
  5496.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  5497.   if (size_pointer) *size_pointer = sbuf.st_size;
  5498.   return 0;
  5499. }
  5500.  
  5501. #ifdef    VMS
  5502.  
  5503. /* Under VMS we need to fix up the "include" specification
  5504.    filename so that everything following the 1st slash is
  5505.    changed into its correct VMS file specification. */
  5506.  
  5507. hack_vms_include_specification (fname)
  5508.      char *fname;
  5509. {
  5510.   register char *cp, *cp1, *cp2;
  5511.   char Local[512];
  5512.   extern char *index (), *rindex ();
  5513.  
  5514.   /* Ignore leading "./"s */
  5515.   while (fname[0] == '.' && fname[1] == '/')
  5516.     strcpy (fname, fname+2);
  5517.   /* Look for the boundary between the VMS and UNIX filespecs */
  5518.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  5519.   if (cp == 0) cp == rindex (fname, '>'); /* ... Ditto            */
  5520.   if (cp == 0) cp == rindex (fname, ':'); /* Look for end of devspec. */
  5521.   if (cp) {
  5522.     cp++;
  5523.   } else {
  5524.     cp = index (fname, '/');    /* Look for the "/" */
  5525.   }
  5526.   /* See if we found that 1st slash */
  5527.   if (cp == 0) return;        /* Nothing to do!!! */
  5528.   if (*cp != '/') return;    /* Nothing to do!!! */
  5529.   /* Point to the UNIX filename part (which needs to be fixed!) */
  5530.   cp1 = cp+1;
  5531.   /* If the directory spec is not rooted, we can just copy
  5532.      the UNIX filename part and we are done */
  5533.   if (((cp - fname) > 2)
  5534.       && ((cp[-1] == ']') || (cp[-1] == '>'))
  5535.       && (cp[-2] != '.')) {
  5536.     strcpy (cp, cp1);
  5537.     return;
  5538.   }
  5539.   /* If there are no other slashes then the filename will be
  5540.      in the "root" directory.  Otherwise, we need to add
  5541.      directory specifications. */
  5542.   if (index (cp1, '/') == 0) {
  5543.     /* Just add "[000000]" as the directory string */
  5544.     strcpy (Local, "[000000]");
  5545.     cp2 = Local + strlen (Local);
  5546.   } else {
  5547.     /* Open the directory specification */
  5548.     cp2 = Local;
  5549.     *cp2++ = '[';
  5550.     /* As long as there are still subdirectories to add, do them. */
  5551.     while (index (cp1, '/') != 0) {
  5552.       /* If this token is "." we can ignore it */
  5553.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  5554.     cp1 += 2;
  5555.     continue;
  5556.       }
  5557.       /* Add a subdirectory spec. */
  5558.       if (cp2 != Local+1) *cp2++ = '.';
  5559.       /* If this is ".." then the spec becomes "-" */
  5560.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  5561.     /* Add "-" and skip the ".." */
  5562.     *cp2++ = '-';
  5563.     cp1 += 3;
  5564.     continue;
  5565.       }
  5566.       /* Copy the subdirectory */
  5567.       while (*cp1 != '/') *cp2++= *cp1++;
  5568.       cp1++;            /* Skip the "/" */
  5569.     }
  5570.     /* Close the directory specification */
  5571.     *cp2++ = ']';
  5572.   }
  5573.   /* Now add the filename */
  5574.   while (*cp1) *cp2++ = *cp1++;
  5575.   *cp2 = 0;
  5576.   /* Now append it to the original VMS spec. */
  5577.   strcpy (cp, Local);
  5578.   return;
  5579. }
  5580. #endif    /* VMS */
  5581.  
  5582. #ifdef    VMS
  5583.  
  5584. /* These are the read/write replacement routines for
  5585.    VAX-11 "C".  They make read/write behave enough
  5586.    like their UNIX counterparts that CCCP will work */
  5587.  
  5588. int
  5589. read (fd, buf, size)
  5590.      int fd;
  5591.      char *buf;
  5592.      int size;
  5593. {
  5594. #undef    read    /* Get back the REAL read routine */
  5595.   register int i;
  5596.   register int total = 0;
  5597.  
  5598.   /* Read until the buffer is exhausted */
  5599.   while (size > 0) {
  5600.     /* Limit each read to 32KB */
  5601.     i = (size > (32*1024)) ? (32*1024) : size;
  5602.     i = read (fd, buf, i);
  5603.     if (i <= 0) {
  5604.       if (i == 0) return (total);
  5605.       return(i);
  5606.     }
  5607.     /* Account for this read */
  5608.     total += i;
  5609.     buf += i;
  5610.     size -= i;
  5611.   }
  5612.   return (total);
  5613. }
  5614.  
  5615. int
  5616. write (fd, buf, size)
  5617.      int fd;
  5618.      char *buf;
  5619.      int size;
  5620. {
  5621. #undef    write    /* Get back the REAL write routine */
  5622.   int i;
  5623.   int j;
  5624.  
  5625.   /* Limit individual writes to 32Kb */
  5626.   i = size;
  5627.   while (i > 0) {
  5628.     j = (i > (32*1024)) ? (32*1024) : i;
  5629.     if (write (fd, buf, j) < 0) return (-1);
  5630.     /* Account for the data written */
  5631.     buf += j;
  5632.     i -= j;
  5633.   }
  5634.   return (size);
  5635. }
  5636.  
  5637. #endif /* VMS */
  5638.